Skip to content

Commit

Permalink
Wire up window close events to dispatcher queue
Browse files Browse the repository at this point in the history
  • Loading branch information
Sergio0694 committed Aug 29, 2024
1 parent d6105a3 commit 3b9bbff
Showing 1 changed file with 10 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,20 @@ public static void Run(Win32Application application, string windowTitle)
}
};

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();

Expand Down

0 comments on commit 3b9bbff

Please sign in to comment.