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)
{
CreateMultipleInstance(callback, args);
CreateMultipleInstance(args);
}
}
catch (AbandonedMutexException)
@ -94,40 +94,54 @@ public void Dispose()
private void CreateFirstInstance(EventHandler<InstanceCallbackEventArgs> 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<InstanceCallbackEventArgs> 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);
}