From 00339bc34cc2293040c6cfebb01a0001fe407a0a Mon Sep 17 00:00:00 2001 From: Anton Kasyanov Date: Sun, 5 May 2019 02:25:26 +0300 Subject: [PATCH] Single-instance check sometimes doesn't detect another running instance --- Eve-O-Preview/Program.cs | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/Eve-O-Preview/Program.cs b/Eve-O-Preview/Program.cs index 16fc709..9ab309b 100644 --- a/Eve-O-Preview/Program.cs +++ b/Eve-O-Preview/Program.cs @@ -11,7 +11,9 @@ namespace EveOPreview { 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; /// The main entry point for the application. [STAThread] @@ -28,11 +30,11 @@ namespace EveOPreview // The very usual Mutex-based single-instance screening // 'token' variable is used to store reference to the instance Mutex // 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 // Nothing to do here - if (token == null) + if (Program._singleInstanceMutex == null) { return; } @@ -46,7 +48,7 @@ namespace EveOPreview controller.Run(); } - private static object GetInstanceToken() + private static Mutex GetInstanceToken() { // The code might look overcomplicated here for a single Mutex operation // Yet we had already experienced a Windows-level issue @@ -55,7 +57,7 @@ namespace EveOPreview // exceptions later try { - Mutex.OpenExisting(Program.MutexName); + Mutex.OpenExisting(Program.MUTEX_NAME); // if that didn't fail then another instance is already running return null; } @@ -65,7 +67,7 @@ namespace EveOPreview } 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; } }