How can I prevent launching my app multiple times?

后端 未结 10 1847
北荒
北荒 2020-12-13 18:42

I deployed my C# WinForms application using ClickOnce installation. Everything works fine with it (after a lot of work) :), but now I\'m facing a problem:

Whenever

10条回答
  •  心在旅途
    2020-12-13 19:01

    Use this code:

    [STAThread]
    static void Main() 
    {
       using(Mutex mutex = new Mutex(false, "Global\\" + appGuid))
       {
          if(!mutex.WaitOne(0, false))
          {
             MessageBox.Show("Instance already running");
             return;
          }
    
          Application.Run(new Form1());
       }
    }
    

    from The Misunderstood Mutex

提交回复
热议问题