From 7a8547208e1d8c535c3ca9ed23029aeaae1fc3d3 Mon Sep 17 00:00:00 2001 From: Jaex Date: Tue, 29 Mar 2016 13:43:30 +0300 Subject: [PATCH] fixed #1462: Try catch IPC issues --- .../ApplicationInstanceManager.cs | 60 ++++++++++++------- 1 file changed, 37 insertions(+), 23 deletions(-) diff --git a/ShareX.HelpersLib/ApplicationInstanceManager.cs b/ShareX.HelpersLib/ApplicationInstanceManager.cs index 9db74887b..4a1a4d939 100644 --- a/ShareX.HelpersLib/ApplicationInstanceManager.cs +++ b/ShareX.HelpersLib/ApplicationInstanceManager.cs @@ -58,7 +58,7 @@ public ApplicationInstanceManager(bool isSingleInstance, string[] args, EventHan if (IsSingleInstance && !IsFirstInstance) { - CreateMultipleInstance(callback, args); + CreateMultipleInstance(args); } } catch (AbandonedMutexException) @@ -94,40 +94,54 @@ public void Dispose() private void CreateFirstInstance(EventHandler callback) { - bool createdNew; - - using (EventWaitHandle eventWaitHandle = new EventWaitHandle(false, EventResetMode.AutoReset, EventName, out createdNew)) + try { - // Mixing single instance and multi instance (via command line parameter) copies of the program can - // result in CreateFirstInstance being called if it isn't really the first one. Make sure this is - // really first instance by detecting if EventWaitHandle was created - if (!createdNew) + bool createdNew; + + using (EventWaitHandle eventWaitHandle = new EventWaitHandle(false, EventResetMode.AutoReset, EventName, out createdNew)) { - return; + // Mixing single instance and multi instance (via command line parameter) copies of the program can + // result in CreateFirstInstance being called if it isn't really the first one. Make sure this is + // really first instance by detecting if EventWaitHandle was created + if (!createdNew) + { + return; + } + + semaphore = new Semaphore(1, 1, SemaphoreName); + ThreadPool.RegisterWaitForSingleObject(eventWaitHandle, WaitOrTimerCallback, callback, Timeout.Infinite, false); + + RegisterRemoteType(AppName); } - - semaphore = new Semaphore(1, 1, SemaphoreName); - ThreadPool.RegisterWaitForSingleObject(eventWaitHandle, WaitOrTimerCallback, callback, Timeout.Infinite, false); - - RegisterRemoteType(AppName); + } + catch (Exception e) + { + DebugHelper.WriteException(e); } } - private void CreateMultipleInstance(EventHandler callback, string[] args) + private void CreateMultipleInstance(string[] args) { - InstanceProxy.CommandLineArgs = args; - - using (EventWaitHandle eventWaitHandle = EventWaitHandle.OpenExisting(EventName)) + try { - semaphore = Semaphore.OpenExisting(SemaphoreName); - semaphore.WaitOne(); - UpdateRemoteObject(AppName); + InstanceProxy.CommandLineArgs = args; - if (eventWaitHandle != null) + using (EventWaitHandle eventWaitHandle = EventWaitHandle.OpenExisting(EventName)) { - eventWaitHandle.Set(); + semaphore = Semaphore.OpenExisting(SemaphoreName); + semaphore.WaitOne(); + UpdateRemoteObject(AppName); + + if (eventWaitHandle != null) + { + eventWaitHandle.Set(); + } } } + catch (Exception e) + { + DebugHelper.WriteException(e); + } Environment.Exit(0); }