Winform(C#)限制程序只运行一个实例

C#控制只运行开启一个程序 

在这个例子中不需要调用ReleaseMutex,mutex会在程序结束时自动释放。为了防止mutex过早释放,在程序的最后调用下GC.KeepAlive (mutex)。

在Programm.cs里面的main函数

加入:Using  System.Diagnostics;

        Using  System.Threading;

static void Main()
        {
            bool instantiated;
            Mutex mutex = new Mutex(true, "UniqueID", out instantiated);
            if (!instantiated)
            {
                MessageBox.Show("程序已经在运行中。。。");
                return;
            }

            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new FrmDelFolder());

            GC.KeepAlive(mutex);
        }

摘自:http://www.lwolf.cn/BLOG/article/code/csharp-winform-run-only-one-copy.htm

原文地址:https://www.cnblogs.com/newmanzhang/p/3181880.html