原理是 启动应用程序后 创建互斥锁 当再启动应用程序后 互斥锁已经存在 就会再次创建失败 就说明 程序启动过
互斥锁 和线程锁类似 不过 互斥锁可以在进程间 通用 线程锁 只能在一个进程里 给不同线程用
这是常用 的限制多开的方法 限制多开可以有非常多的方法
using System.Threading;
using System;
class MyClass
{
//程序启动时判断进程是否可以启动
private void App()
{
if (!CanCreate())
{
logger.Warn("Can't run the Environment now, because one Instance is already running!");
Environment.Exit(-1);
}
}
//进程是否可以创建
private static bool CanCreate()
{
bool canCreate;
mutex = new Mutex(true, "name", out canCreate);
return canCreate;
}
}
网友评论