Detecting Multiple Instance in Windows Application

  1. using (Mutex mutex = new Mutex(false"mutex name"))  
  2. {  
  3.     if (!mutex.WaitOne(0, true))  
  4.     {  
  5.         System.Threading.Thread.Sleep(5000);  
  6.         MessageBox.Show("Unable to run multiple instances of this program.",  
  7.              "Error",  
  8.              MessageBoxButtons.OK,  
  9.              MessageBoxIcon.Error);  
  10.     }  
  11.     else  
  12.     {  
  13.         System.Threading.Thread.Sleep(5000);  
  14.         MessageBox.Show("first click");  
  15.         //Application.EnableVisualStyles();  
  16.         //Application.SetCompatibleTextRenderingDefault(false);  
  17.         //Application.Run(new MainForm());  
    }