测试函数:
privateboolAppAlreadyRunning()

...{
System.Diagnostics.ProcesscurProcess=System.Diagnostics.Process.GetCurrentProcess();
System.Diagnostics.Process[]allProcess=System.Diagnostics.Process.GetProcesses();
foreach(System.Diagnostics.ProcessprocessinallProcess)

...{
if(process.Id!=curProcess.Id)

...{
if(process.ProcessName==curProcess.ProcessName)
returntrue;
}
}
returnfalse;
}
System.Diagnostics.Process[]pros=
System.Diagnostics.Process.GetProcessesByName(
System.Diagnostics.Process.GetCurrentProcess().ProcessName);
if(pros.Length>1)

...{
Application.Exit();
return;
}
privateboolAppAlreadyRunning()
...{
System.Diagnostics.ProcesscurProcess=System.Diagnostics.Process.GetCurrentProcess();
System.Diagnostics.Process[]allProcess=System.Diagnostics.Process.GetProcesses();
foreach(System.Diagnostics.ProcessprocessinallProcess)
...{
if(process.Id!=curProcess.Id)
...{
if(process.ProcessName==curProcess.ProcessName)
returntrue;
}
}
returnfalse;
}
应用程序中直接判断:
System.Diagnostics.Process[]pros=
System.Diagnostics.Process.GetProcessesByName(
System.Diagnostics.Process.GetCurrentProcess().ProcessName);
if(pros.Length>1)
...{
Application.Exit();
return;
}
在此向提供代码参照的同行表示感谢!
检测应用重复运行
本文提供了两种检查当前应用程序是否已有一个实例正在运行的方法:通过遍历进程列表并比较进程名称,或者直接获取当前进程名称相同的进程数量。
271

被折叠的 条评论
为什么被折叠?



