C#设定只能开一个实例

来源:互联网 发布:nginx如何做负载均衡 编辑:程序博客网 时间:2024/06/05 19:48

当有时候需要c#程序只能开一个的时候怎么办呢,只要在程序的Program.cs的main方法里面粘贴以下代码就行了

bool createNew;            using (System.Threading.Mutex mutex = new System.Threading.Mutex(true, Application.ProductName, out createNew))            {                if (createNew)                {                    Application.Run(new Form2());                }                else                {                    MessageBox.Show("应用程序已经在运行中...");                    //System.Threading.Thread.Sleep(1000);                    System.Environment.Exit(1);                }            }

本人亲测有效!!!

原创粉丝点击