您的位置:首页 > 其它

winform实现让程序只能打开一个实例(总结3方法)

2013-07-23 16:28 666 查看
代码:
//=====创建互斥体法:=====

//bool blnIsRunning;

//Mutex mutexApp = new Mutex(false, Assembly.GetExecutingAssembly().FullName, out   blnIsRunning);

//if (!blnIsRunning)

//{

//    MessageBox.Show("程序已经运行!", "提示",

//    MessageBoxButtons.OK, MessageBoxIcon.Exclamation);

//    return;

//}


代码:
//保证同时只有一个客户端在运行

//System.Threading.Mutex mutexMyapplication = new System.Threading.Mutex(false, "OnePorcess.exe");

//if (!mutexMyapplication.WaitOne(100, false))

//{

//    MessageBox.Show("程序" + Application.ProductName + "已经运行!", Application.ProductName,

//    MessageBoxButtons.OK, MessageBoxIcon.Error);

//    return;

//}


代码:
//=====判断进程法:(修改程序名字后依然能执行)=====

//Process current = Process.GetCurrentProcess();

//Process[] processes = Process.GetProcessesByName(current.ProcessName);

//foreach (Process process in processes)

//{

//    if (process.Id != current.Id)

//    {

//        if (process.MainModule.FileName

//        == current.MainModule.FileName)

//        {

//            MessageBox.Show("程序已经运行!", Application.ProductName,

//            MessageBoxButtons.OK, MessageBoxIcon.Exclamation);

//            return;

//        }

//    }

//}

只需要把需要的方法代码放在Void Main()方法中就可以实现..
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: