giammin

Avoid multiple application instances

Sep 17th, 2013
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.41 KB | None | 0 0
  1. //reference http://odetocode.com/blogs/scott/archive/2004/08/20/the-misunderstood-mutex.aspx
  2.  
  3. [STAThread]
  4. static void Main()
  5. {
  6.    using(Mutex mutex = new Mutex(false, @"Global\" + appGuid))
  7.    {
  8.       if(!mutex.WaitOne(0, false))
  9.       {
  10.          MessageBox.Show("Instance already running");
  11.          return;
  12.       }
  13.    
  14.       GC.Collect();                
  15.       Application.Run(new Form1());
  16.    }
  17. }
Advertisement
Add Comment
Please, Sign In to add comment