fixed #1462: Try catch IPC issues

This commit is contained in:
Jaex 2016-03-29 13:43:30 +03:00
parent 830d6b2aec
commit 7a8547208e

View file

@ -58,7 +58,7 @@ public ApplicationInstanceManager(bool isSingleInstance, string[] args, EventHan
if (IsSingleInstance && !IsFirstInstance) if (IsSingleInstance && !IsFirstInstance)
{ {
CreateMultipleInstance(callback, args); CreateMultipleInstance(args);
} }
} }
catch (AbandonedMutexException) catch (AbandonedMutexException)
@ -94,40 +94,54 @@ public void Dispose()
private void CreateFirstInstance(EventHandler<InstanceCallbackEventArgs> callback) private void CreateFirstInstance(EventHandler<InstanceCallbackEventArgs> callback)
{ {
bool createdNew; try
using (EventWaitHandle eventWaitHandle = new EventWaitHandle(false, EventResetMode.AutoReset, EventName, out createdNew))
{ {
// Mixing single instance and multi instance (via command line parameter) copies of the program can bool createdNew;
// 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 using (EventWaitHandle eventWaitHandle = new EventWaitHandle(false, EventResetMode.AutoReset, EventName, out createdNew))
if (!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); catch (Exception e)
ThreadPool.RegisterWaitForSingleObject(eventWaitHandle, WaitOrTimerCallback, callback, Timeout.Infinite, false); {
DebugHelper.WriteException(e);
RegisterRemoteType(AppName);
} }
} }
private void CreateMultipleInstance(EventHandler<InstanceCallbackEventArgs> callback, string[] args) private void CreateMultipleInstance(string[] args)
{ {
InstanceProxy.CommandLineArgs = args; try
using (EventWaitHandle eventWaitHandle = EventWaitHandle.OpenExisting(EventName))
{ {
semaphore = Semaphore.OpenExisting(SemaphoreName); InstanceProxy.CommandLineArgs = args;
semaphore.WaitOne();
UpdateRemoteObject(AppName);
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); Environment.Exit(0);
} }