FuYGの博客

菜鸟到鸵鸟之质的飞跃

0%

C#程序只允许运行一个实例


C#程序只允许运行一个实例

互斥进程(程序), 简单点说,就是在系统中只能有该程序的一个实例运行.

使用线程互斥变量. 通过定义互斥变量来判断是否已运行实例.C#实现如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
System.Threading.Mutex mutex;

private void CheckExe()
{
bool ret;

//ElectronicNeedleTherapySystem互斥体名称随便定义
mutex = new System.Threading.Mutex(true, "ElectronicNeedleTherapySystem", out ret);
if (!ret)
{
MessageBox.Show("已有一个程序实例运行,请勿重新运行");
Environment.Exit(0);
}
}


程序中通过语句:

System.Threading.Mutex mutex = new System.Threading.Mutex(true, "ElectronicNeedleTherapySystem", out ret)

来申明一个互斥体变量mutex,其中ElectronicNeedleTherapySystem为互斥体名,布尔变量ret用来保存是否已经运行了该程序事例.