c# - Checking if a MainWindow already exists -
I have a C # / WPF application that has a main window and system tray icon when I close the main window To keep an application running in the system tray, configure App.xaml with ShutdownMode = "OnExplicitShutdown". In the context menu of the system tray icon, I have the menu items binding for ways to reopen the main window:
Private Zero SysTray_Information_Click (Object Sender, RoutedEventArgs E) {var newWindow = new MainWindow (); NewWindow.Show (); MainWindow.Focus (); } I would like to add these methods to see if the main window is already being displayed (not closed). Otherwise it would be possible to open many copies of the main window. How can I get this?
At the application level a semaphore or mute x would be appropriate This code is for a winfroms app, but me Be sure that it can be modified to suit your needs. Fixed Zero Main () {System.Threading.Mutex appMutex = New System. threading. Mute x (true, "MyApplicationName", Exclusive outside); If (! Exclusive) {MessageBox.Show ("Another example of my program is already running", "MyApplicationName", message box button. OK, message box icon. Exclamation); Return; } Application.Run (new FMMAP ()); GC.KeepAlive (appMutex); }
Comments
Post a Comment