Added #4 - only one instance will be allowed to run

This commit is contained in:
Szymon Porwolik
2019-02-28 23:06:38 +01:00
committed by simpo
parent 69f995bd6d
commit f383f3c056
4 changed files with 64 additions and 12 deletions

View File

@@ -1,4 +1,5 @@
using System;
using System.Threading;
using System.Windows.Forms;
namespace Perun_v1
@@ -9,24 +10,39 @@ namespace Perun_v1
/// <summary>
/// Główny punkt wejścia dla aplikacji.
/// </summary>
// Mutex to block multiple instances
static Mutex mutex = new Mutex(true, "{5a710507-92e9-4664-9b91-918b8b82f107}");
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.ApplicationExit += new EventHandler(Application_ApplicationExit);
void Application_ApplicationExit(object sender, EventArgs e)
if (mutex.WaitOne(TimeSpan.Zero, true))
{
Properties.Settings.Default.Save();
}
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.ApplicationExit += new EventHandler(Application_ApplicationExit);
Application.Run(new form_Main());
void Application_ApplicationExit(object sender, EventArgs e)
{
Properties.Settings.Default.Save();
}
Application.Run(new form_Main());
mutex.ReleaseMutex();
}
else
{
// send our Win32 message to make the currently running instance
// jump on top of all the other windows
NativeMethods.PostMessage(
(IntPtr)NativeMethods.HWND_BROADCAST,
NativeMethods.WM_SHOWME,
IntPtr.Zero,
IntPtr.Zero);
MessageBox.Show("Only one instance is allowed.");
}
}
}
}