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

来源:互联网 发布:网络司法拍卖变卖 编辑:程序博客网 时间:2024/06/05 11:08
代码:
                        //=====创建互斥体法:=====
            //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()方法中就可以实现..