方案一: //判断是否已经存在一个exe 是要写在main函数里面的 [STAThread] static void Main() { bool createdNew; Mutex m = new Mutex(true, "yourexe", out createdNew); if (! createdNew) { MessageBox.Show("Only one exe is allowed at a time."); return; } Application.Run(new Start()); GC.KeepAlive(m); }
上面的要添加using System.Threading; Mutex class是用于进程同步的 方案二: using System.Diagnostics;
//判断是否已经存在一个exe
是要写在main函数里面的
[STAThread]
static void Main()
{
bool createdNew;
Mutex m = new Mutex(true, "yourexe", out createdNew);
if (! createdNew)
{
MessageBox.Show("Only one exe is allowed at a time.");
return;
}
Application.Run(new Start());
GC.KeepAlive(m);
}
上面的要添加using System.Threading;
Mutex class是用于进程同步的
方案二:
using System.Diagnostics;
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
// 防止多次执行
Process[] processes = Process.GetProcessesByName("RCL");
if (processes.Length >= 2 )
{
MessageBox.Show("程序已经执行!","提醒",MessageBoxButtons.OK,MessageBoxIcon.Information);
return;
}
else
Application.Run(new frmMain());
}