在入口文件Program.cs中添加如下代码:
static class Program
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
bool Exist = false;
System.Diagnostics.Process CIP = System.Diagnostics.Process.GetCurrentProcess();
System.Diagnostics.Process[] CIPR = System.Diagnostics.Process.GetProcesses();
foreach (System.Diagnostics.Process p in CIPR)
{
if (p.ProcessName == CIP.ProcessName && p.Id!=CIP.Id)
{
Exist = true;
}
}
if (Exist)
{
return;
}
else
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new MainInterface());
}
}
}
防止应用多实例
本文介绍了一种防止Windows应用程序启动多个实例的方法。通过检查当前运行进程,确保只存在一个应用程序实例,有效避免资源浪费和用户体验下降。
3363

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



