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

相关文章:

  • 2022-01-14
  • 2021-10-17
  • 2022-12-23
  • 2021-06-24
  • 2022-12-23
  • 2022-02-01
猜你喜欢
  • 2022-02-09
  • 2022-03-05
  • 2022-01-24
  • 2022-12-23
  • 2022-02-23
  • 2021-12-24
相关资源
相似解决方案