Few small ApplicationInstanceManager changes

This commit is contained in:
Jaex 2016-03-20 12:44:20 +02:00
parent ef6eb94e28
commit 75d59c5a8c

View file

@ -34,27 +34,31 @@ namespace ShareX.HelpersLib
{ {
public class ApplicationInstanceManager : IDisposable public class ApplicationInstanceManager : IDisposable
{ {
private static string AppName = "ShareX"; private static readonly string MutexName = "82E6AC09-0FEF-4390-AD9F-0DD3F5561EFC";
private static string EventName = string.Format("{0}-{1}", Environment.MachineName, AppName); private static readonly string AppName = "ShareX";
private static string SemaphoreName = string.Format("{0}{1}", EventName, "Semaphore"); private static readonly string EventName = string.Format("{0}-{1}", Environment.MachineName, AppName);
private static readonly string SemaphoreName = string.Format("{0}{1}", EventName, "Semaphore");
public bool IsSingleInstance { get; private set; }
public bool IsFirstInstance { get; private set; } public bool IsFirstInstance { get; private set; }
private Mutex mutex; private Mutex mutex;
private Semaphore semaphore; private Semaphore semaphore;
private IpcServerChannel serverChannel; private IpcServerChannel serverChannel;
public ApplicationInstanceManager(bool isMultiInstance, string[] args, EventHandler<InstanceCallbackEventArgs> singleInstanceCallback) public ApplicationInstanceManager(bool isSingleInstance, string[] args, EventHandler<InstanceCallbackEventArgs> callback)
{ {
mutex = new Mutex(false, "82E6AC09-0FEF-4390-AD9F-0DD3F5561EFC"); // Specific mutex required for installer IsSingleInstance = isSingleInstance;
mutex = new Mutex(false, MutexName);
try try
{ {
IsFirstInstance = mutex.WaitOne(100, false); IsFirstInstance = mutex.WaitOne(100, false);
if (!IsFirstInstance && !isMultiInstance) if (IsSingleInstance && !IsFirstInstance)
{ {
CreateMultipleInstance(singleInstanceCallback, args); CreateMultipleInstance(callback, args);
} }
} }
catch (AbandonedMutexException) catch (AbandonedMutexException)
@ -64,7 +68,7 @@ public ApplicationInstanceManager(bool isMultiInstance, string[] args, EventHand
IsFirstInstance = true; IsFirstInstance = true;
} }
CreateFirstInstance(singleInstanceCallback); CreateFirstInstance(callback);
} }
public void Dispose() public void Dispose()
@ -186,11 +190,11 @@ public void SetCommandLineArgs(string[] commandLineArgs)
public class InstanceCallbackEventArgs : EventArgs public class InstanceCallbackEventArgs : EventArgs
{ {
public string[] CommandLineArgs { get; private set; }
internal InstanceCallbackEventArgs(string[] commandLineArgs) internal InstanceCallbackEventArgs(string[] commandLineArgs)
{ {
CommandLineArgs = commandLineArgs; CommandLineArgs = commandLineArgs;
} }
public string[] CommandLineArgs { get; private set; }
} }
} }