Single-instance check sometimes doesn't detect another running instance

This commit is contained in:
Anton Kasyanov
2019-05-05 02:25:26 +03:00
parent ef9924b164
commit 00339bc34c

View File

@@ -11,7 +11,9 @@ namespace EveOPreview
{ {
static class Program static class Program
{ {
private static string MutexName = "EVE-O Preview Single Instance Mutex"; private static string MUTEX_NAME = "EVE-O Preview Single Instance Mutex";
private static Mutex _singleInstanceMutex;
/// <summary>The main entry point for the application.</summary> /// <summary>The main entry point for the application.</summary>
[STAThread] [STAThread]
@@ -28,11 +30,11 @@ namespace EveOPreview
// The very usual Mutex-based single-instance screening // The very usual Mutex-based single-instance screening
// 'token' variable is used to store reference to the instance Mutex // 'token' variable is used to store reference to the instance Mutex
// during the app lifetime // during the app lifetime
object token = Program.GetInstanceToken(); Program._singleInstanceMutex = Program.GetInstanceToken();
// If it was not possible to acquire the app token then another app instance is already running // If it was not possible to acquire the app token then another app instance is already running
// Nothing to do here // Nothing to do here
if (token == null) if (Program._singleInstanceMutex == null)
{ {
return; return;
} }
@@ -46,7 +48,7 @@ namespace EveOPreview
controller.Run<MainFormPresenter>(); controller.Run<MainFormPresenter>();
} }
private static object GetInstanceToken() private static Mutex GetInstanceToken()
{ {
// The code might look overcomplicated here for a single Mutex operation // The code might look overcomplicated here for a single Mutex operation
// Yet we had already experienced a Windows-level issue // Yet we had already experienced a Windows-level issue
@@ -55,7 +57,7 @@ namespace EveOPreview
// exceptions later // exceptions later
try try
{ {
Mutex.OpenExisting(Program.MutexName); Mutex.OpenExisting(Program.MUTEX_NAME);
// if that didn't fail then another instance is already running // if that didn't fail then another instance is already running
return null; return null;
} }
@@ -65,7 +67,7 @@ namespace EveOPreview
} }
catch (Exception) catch (Exception)
{ {
Mutex token = new Mutex(true, Program.MutexName, out var result); Mutex token = new Mutex(true, Program.MUTEX_NAME, out var result);
return result ? token : null; return result ? token : null;
} }
} }