背景
检查是否重复运行程序,避免不必要的麻烦。
结果
实现
bool vExist;
System.Threading.Mutex newMutex = new System.Threading.Mutex(true, "test", out vExist);
if (!vExist)
{
// 程序已启动
DialogResult dr = MessageBox.Show(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "\r\n当前已有一个程序正在运行!" + "\r\n确认运行?",
"提示",
MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
if (dr != DialogResult.OK)
{
return;
}
}