static void Main() { try { Mutex existingInstane = Mutex.OpenExisting(mutexName); InteropMethods.PostMessage((IntPtr)InteropMethods.HWND_BROADCAST, InteropMethods.WM_SHOWME, IntPtr.Zero, IntPtr.Zero); if (Properties.Settings.Default.IsRestarting) { existingInstane.ReleaseMutex(); Properties.Settings.Default.IsRestarting = true; Properties.Settings.Default.Save(); throw new Exception("Restart requested, release mutex"); } return; } catch { using (Mutex instanceMutex = new Mutex(true, mutexName, out _)) { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); //SplashForm.ShowSplashScreen(); Application.Run(new MainForm()); GC.KeepAlive(instanceMutex); } } }
protected override void OnStartup(StartupEventArgs e) { instanceMutex = new Mutex(true, mutexName, out bool createdNew); if (!createdNew) { // App is already running! Exit the application and show the other window. Current.Shutdown(); InteropMethods.PostMessage((IntPtr)InteropMethods.HWND_BROADCAST, InteropMethods.WM_SHOWME, IntPtr.Zero, IntPtr.Zero); } else { GC.KeepAlive(instanceMutex); base.OnStartup(e); } }
protected override void OnStartup(StartupEventArgs e) { instanceMutex = new Mutex(true, mutexName, out createdNew); if (!createdNew) { // App is already running! Exit the application and show the other window. InteropMethods.PostMessage((IntPtr)InteropMethods.HWND_BROADCAST, InteropMethods.WM_SHOWME, IntPtr.Zero, IntPtr.Zero); Current.Shutdown(); Environment.Exit(0); } updater = new Updater(); GC.KeepAlive(instanceMutex); SplashWindow.Start(); base.OnStartup(e); }