From 54003cd6a366173a3893514c803ee62d605821eb Mon Sep 17 00:00:00 2001 From: Glenn Maynard Date: Sun, 6 Oct 2019 03:58:20 -0500 Subject: [PATCH] Add an SMXConfigShutdown event to allow the installer to close the application automatically. --- smx-config/App.xaml.cs | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/smx-config/App.xaml.cs b/smx-config/App.xaml.cs index 70479e4..9de2bc3 100644 --- a/smx-config/App.xaml.cs +++ b/smx-config/App.xaml.cs @@ -30,6 +30,9 @@ namespace smx_config return; } + // This is used by the installer to close a running instance automatically when updating. + ListenForShutdownRequest(); + // If we're being launched on startup, but the LaunchOnStartup setting is false, // then the user turned off auto-launching but we're still being launched for some // reason (eg. a renamed launch shortcut that we couldn't find to remove). As @@ -181,6 +184,23 @@ namespace smx_config })); } + private void ListenForShutdownRequest() + { + // We've already checked that we're the only instance when we get here, so this event shouldn't + // exist. If it already exists for some reason, we'll listen to it anyway. + EventWaitHandle SMXConfigShutdown = new EventWaitHandle(false, EventResetMode.AutoReset, "SMXConfigShutdown"); + ThreadPool.RegisterWaitForSingleObject(SMXConfigShutdown, ShutdownApplicationCallback, this, Timeout.Infinite, false); + } + + private static void ShutdownApplicationCallback(Object self, Boolean timedOut) + { + // This is called when another instance sends us a message over SMXConfigShutdown. + Application.Current.Dispatcher.Invoke(new Action(() => { + App application = (App) Application.Current; + application.Shutdown(); + })); + } + // Create a tray icon. For some reason there's no WPF interface for this, // so we have to use Forms. void CreateTrayIcon()