Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which of the below Mutex expressions ideally prevents multiple instances of .Net application and what is the difference?

Typically I see these two pieces of code all around. Both works in my case too, but which should I stick to?

Case 1:

bool isNew = false;
Mutex mutex = new Mutex(true, "MyApp_Mutex", out isNew);
if (!isNew)
{
    MessageBox.Show("already running.", "Multiple Instances Not Allowed",
                                        MessageBoxButtons.OK, 
                                        MessageBoxIcon.Exclamation);
    return;
}

Case 2:

Mutex mutex = new Mutex(false, "MyApp_Mutex"))            
if (!mutex.WaitOne(0, false))
{
    MessageBox.Show("already running.", "Multiple Instances Not Allowed", 
                                        MessageBoxButtons.OK, 
                                        MessageBoxIcon.Exclamation);
    return;
}
  1. Which is the ideal way between the two to prevent multiple instances?

  2. What is the difference?

  3. Moreover I see codes like these:

    //if not return{
    mutex.ReleaseMutex(); 
    GC.Collect(); 
    //application.Run();
    GC.KeepAlive(mutex); 
    

under the second method but never with the first. Why is that so? Or did I get that wrong?

Basically it lies with the proper understanding of the parameters and methods used. I would appreciate if someone can briefly detail it, I understand not half when reading msdn documentation..

like image 687
nawfal Avatar asked Aug 24 '11 15:08

nawfal


1 Answers

In the first case, you're asking the OS to create the mutex and give you ownership of it if it's created - this is done through the first parameter, initiallyOwned. The isNew parameter tells you whether or not it was a new mutex. If it's new then you're guaranteed to have ownership of it since that's what you asked for with the initiallyOwned parameter. Since it's new, and you own it, you know there are no other instances of the application running, because if there were, they would have already created the mutex and they would own it.

The second case is basically the same thing, but done in a slightly different way. It's not requesting ownership on the create, but on the WaitOne call. WaitOne is requesting ownership, and waiting for 0 milliseconds. If you get ownership then you know no other instance of your app is running, for the same reasons as case 1.

As for which to use, to my knowledge it doesn't matter. The first seems to be more intuitive, at least to me.

Adding answer to new question #3

When the app is complete it should release the mutex since it owns it. .NET will likely release it for you when your app exits, but it's good practice to do it. GC.Collect and GC.KeepAlive are dealing with garbage collection. I can't think of any scenario why these calls would be needed when dealing with the mutexes controlling startup. I declare my mutex as static, so it will always be in scope and won't be freed by the garbage collector for the lifetime of my app.

like image 105
Dave Carlile Avatar answered Sep 22 '22 02:09

Dave Carlile