C#代码确保应用程序只有一个实例

C#代码确保应用程序只有一个实例

使用互斥锁,实现只有一个程序

代码:

code.cs
1
2
3
4
5
6
7
8
9
10
string appGuid = ((GuidAttribute)Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(GuidAttribute), false).GetValue(0)).Value;
using (Mutex mutex = new Mutex(false, "Global\\" + appGuid))
{
if (!mutex.WaitOne(0, false))
{
return;
}
//启动程序
Application.Run(new Form1());
}

运行环境

Win10
.NET Framework 4.5
VS2019

参考资料:

作者

zhang

发布于

2021-07-20

更新于

2023-09-19

许可协议

CC BY-NC-SA 4.0

Your browser is out-of-date!

Update your browser to view this website correctly.&npsb;Update my browser now

×