【单例】使用同步基元变量来检测程序是否已运行

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
static class Program
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
var instance = new Mutex(true, "SingleStart", out bool createdNew); //同步基元变量
if (createdNew)
{
Application.Run(new Form());
instance.ReleaseMutex();
}
else
{
Application.Exit();
}
}
}

欢迎关注我的其它发布渠道