-
Notifications
You must be signed in to change notification settings - Fork 0
/
SingletonHelper.cs
33 lines (31 loc) · 1001 Bytes
/
SingletonHelper.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
using System;
using System.Threading;
namespace Flithor_ReusableCodes
{
/// <summary>
/// Singleton helper class. Call <see cref="InstanceExists"/> check any running instance exists
/// </summary>
public static class SingletonHelper
{
private static readonly string SINGLETON_ID = GetAssebmlyName();
private static Mutex singletonMutex = new Mutex(true, SINGLETON_ID);
/// <summary>
/// Is any running instance exists
/// </summary>
public static bool InstanceExists => GetInstanceExists();
private static bool GetInstanceExists()
{
if (singletonMutex.WaitOne(TimeSpan.Zero, true))
{
singletonMutex.ReleaseMutex();
return false;
}
return true;
}
private static string GetAssebmlyName()
{
var assembly = typeof(SingletonHelper).Assembly;
return assembly.GetName().Name;
}
}
}