//返回是否赋予了使用线程的互斥体初始所属权
Boolean createdNew;
Mutex instance = new Mutex(true, "MutexName", out createdNew);
//赋予了线程初始所属权,也就是首次使用互斥体
if (createdNew)
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
instance.ReleaseMutex();
Environment.Exit(0);
}
else
{
MessageBox.Show("已经启动了一个程序,请先退出!", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
Application.Exit();
}
当<=1时 只能打开一个实例
//if (System.Diagnostics.Process.GetProcessesByName(System.Diagnostics.Process.GetCurrentProcess().ProcessName).Length <= 1)
//{
// Application.EnableVisualStyles();
// Application.SetCompatibleTextRenderingDefault(false);
// Application.Run(new Form1());
// Environment.Exit(0);
//}
//else
//{
// //MessageBox.Show("程序已经打开!");
//}