Thursday, May 10, 2012

Prevent duplicate execute of my program ?

I use the following code in the program.cs - file in C# 2.0:
     [STAThread]
  
     static void Main()
  
     {
       Application.EnableVisualStyles();
       Application.SetCompatibleTextRenderingDefault(false);
       bool createdNew;
       // To prevent the program to be started twice
       ///Create new mutex
System.Threading.Mutex appMutex = new System.Threading.Mutex (true, Application.ProductName, out createdNew);
       ///if creation of mutex is successful
       if (createdNew)
    {
      Application.Run(new frmServer());
      appMutex.ReleaseMutex();
       }
    else
    {
    /// if the app's already running
string msg = String.Format("The Program \"{0}\" is already running", Application.ProductName);
MessageBox.Show(msg, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Information);
  
      }
       }  

No comments:

Post a Comment