-
Notifications
You must be signed in to change notification settings - Fork 0
/
SingleInstance.cs
36 lines (29 loc) · 949 Bytes
/
SingleInstance.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
using System;
using System.Threading;
using System.Reflection;
namespace PlanSwitcher {
/// <summary>
/// Utilities for restricting application to a single instance.
/// </summary>
public static class SingleInstance {
private static Mutex mutex;
/// <summary>
/// Returns true iff this instance is the only one running.
/// </summary>
public static bool Start() {
bool onlyInstance = false;
string assemblyName = Assembly.GetExecutingAssembly().GetName().Name;
// Note: using local mutex, so multiple instantiations are still
// possible across different sessions.
string mutexName = String.Format("Local\\{0}", assemblyName);
mutex = new Mutex(true, mutexName, out onlyInstance);
return onlyInstance;
}
/// <summary>
/// Marks the current instance as not running.
/// </summary>
static public void Stop() {
mutex.ReleaseMutex();
}
}
}