From 3b9bbffacd0c06593a341587e7d771a9eedb4ed6 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Wed, 28 Aug 2024 19:49:38 -0700 Subject: [PATCH] Wire up window close events to dispatcher queue --- .../Backend/Win32ApplicationRunner.cs | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/samples/ComputeSharp.SwapChain.D2D1.Cli/Backend/Win32ApplicationRunner.cs b/samples/ComputeSharp.SwapChain.D2D1.Cli/Backend/Win32ApplicationRunner.cs index 960432e0..7b33e450 100644 --- a/samples/ComputeSharp.SwapChain.D2D1.Cli/Backend/Win32ApplicationRunner.cs +++ b/samples/ComputeSharp.SwapChain.D2D1.Cli/Backend/Win32ApplicationRunner.cs @@ -72,11 +72,20 @@ internal static unsafe class Win32ApplicationRunner } }; + DispatcherQueueController dispatcherQueueController = DispatcherQueueController.CreateOnCurrentThread(); + + // Bind the window to a 'DispatcherQueue' instance, so it can flow the exit message + appWindow.AssociateWithDispatcherQueue(dispatcherQueueController.DispatcherQueue); + + // Enqueue the exit message when the window is closed. This ensures that + // the process actually terminates after closing the window, as expected. + appWindow.Closing += static (s, e) => s.DispatcherQueue.EnqueueEventLoopExit(); + // Display the window appWindow.Show(activateWindow: true); // Process any messages in the queue - DispatcherQueueController.CreateOnCurrentThread().DispatcherQueue.RunEventLoop(DispatcherRunOptions.ContinueOnQuit, deferral: null); + dispatcherQueueController.DispatcherQueue.RunEventLoop(DispatcherRunOptions.QuitOnlyLocalLoop, deferral: null); tokenSource.Cancel();