检查具有多个互斥锁的多实例可用性

  • 本文关键字:实例 可用性 c#
  • 更新时间 :
  • 英文 :


我正在运行两个彼此并行的任务,在这里我正在运行一个任务,从他的program.cs开始,当它进入form.cs时,我正在启动其中另一个exe的新应用程序。

我正在使用互斥锁来检查它的实例。我在program.cs中使用互斥锁并添加另一个互斥锁来检查它的另一个 exe 实例,但它显示已经很糟糕了。.我将尝试用代码解释..

在首次申请的程序.cs中

  static Mutex mutex = new Mutex(true, "{sdfssdfdsf-B9A1-45fd-A8CF-72F04E6BDE8F}");
 Mutex m = new System.Threading.Mutex(true, "asa", out ok);
            if (!ok)
            {
                if (MessageBox.Show("Another Instance Is Already running of This Application", "Cyber Security", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    MessageBox.Show("System is exiting.", "Systems");
                    Application.ExitThread();
                    return;
                }
            }

在形式上.cs

 static Mutex mutex = new Mutex(true, "{asdasd-B9A1-45fd-A8CF-asfsadf}");
 Mutex m = new System.Threading.Mutex(true, "ASDA", out ok);
                if (!ok)
                {
                    if (MessageBox.Show("Another Instance Is Already running of This Application", "log", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                    {
                        MessageBox.Show("System is exiting.", "Log");
                        Application.ExitThread();
                        return;
                    }
                }

//but this show thee error the second instance is already ruunning?
 Mutex m = new System.Threading.Mutex(true, "asa", out ok);
 Mutex m = new System.Threading.Mutex(true, "ASDA", out ok);

它们都需要相同的名称

 Mutex m = new System.Threading.Mutex(true, "ASDA", out ok);

应该是

 Mutex m = new System.Threading.Mutex(true, "asa", out ok);
                                              ^-------------------Change

最新更新