Sep 20, 2011

Allow only one instance of C# application running

using System.Threading;

but the simplest and least intrusive one for me is:



Basically this is what I use in my Program.cs:
static System.Threading.Mutex s_mutex = null;

[STAThread] static void Main()
{
    bool instantiated;
    s_mutex = new System.Threading.Mutex(false, "Binglong.My.Application.Mutex", out instantiated);
    if (instantiated)
    {   // If instantiated is true, this is the first instance of the application
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new MyApplicationForm());
    }
      else
            {
                MessageBox.Show("alreddy running");
            }

}

No comments:

Post a Comment