本文最后更新于 174 天前,其中的信息可能已经有所发展或是发生改变。
using System.Reflection;
using System.Threading;
public enum MutexScope
{
Local,
Global
}
public static class SingletonApp
{
private static Mutex? mutex;
public static bool IsNew { get; private set; }
public static bool Check()
{
return Check(MutexScope.Local);
}
public static bool Check(MutexScope scope)
{
return Check($"{scope}\\{Assembly.GetEntryAssembly()?.GetName().Name}");
}
public static bool Check(string name)
{
bool createdNew;
mutex = new(true, name, out createdNew);
IsNew = createdNew;
return createdNew;
}
public static void Cleanup()
{
mutex?.Close();
}
}