Example of how to make sure only one instance of your program is running.

I needed a way to ensure that only one instance of my program was running at any given time. So after a little googling this is what I came up with:

Here is some sample code that can be used to ensure that only one instance of a program is running.
public static void Main(string[] args)
{
  // bool used to determine if we created a new mutex
  bool createdNew;
 
  // give a unique name for the mutex,
  // prefix it with Local\ to ensure that it's created in the per-session namespace, not the global namespace.

  string mutexName = @"Local\" + System.Reflection.Assembly.GetExecutingAssembly().GetName().Name;     // Create a new mutex object with given name   Mutex m = new Mutex(false, mutexName, out createdNew);     // If the mutex already exists then don't start program becase   // another instance is already running.   if(!createdNew)     return;     //   // Do work like Application.Run(new MainForm());   //     // Release the mutex resources   m.ReleaseMutex(); }

This little trick seems to work for my needs.

Update: Added "Local\" to the mutex name per Shawn's suggestion

6 Comments

  • Sendhil,


    I also wanted to make sure that any href-exe(smart clients) could only be ran once as well. In there case the process name shows up as IEExec.exe, I believe and if so then there could be other instances of that running with out your program actually running. Just a potential problem I don't know if it actually is.





    Thanks for the Info Shawn I will look into it and test this out.





    Wes

  • Didn't work when I tried it because you have a rather bizzare coding error in the source on this page.



    It has to be this instead:



    // Create a new mutex object with given name

    Mutex m = new Mutex(TRUE, mutexName, out createdNew);



    Or the thread will not actually own the code and teh whole thing will fail. (I checked the help when it failed and that first bool in the call to new Mutex says whether you want to own it immediately from the calling thread. ..Well that's the whole point really isn't it so of course you do i.e. true not false)



    Strange you have the code 99% correct like that!





    John

  • Actually I had problems with this too, and I fixed it by putting the Release in try catch block and didn't think to post about that. Thanks for the heads up to other people though.

  • I agree Jon, the parameter should be false, not true.



    On the other point, in my development environment a run time error occurs if you do not explicitly release the mutex.



    Again, I have no idea why, but perhaps it has something to do with the indeterministic nature of the garbage collector?

  • string mutexName = "Global\\" + System.Reflection.Assembly.GetExecutingAssembly().GetName().Name;

    just for other not search, as I'm.

  • The reason that release is throwing an execption is that release releases ownership, and we have not do not own the mutex. Close is appropriate since we want the garbage collector to remove this mutext from the system-wide name space as soon as possible. Close should be called regardless of whether createNew is true or false.

Comments have been disabled for this content.