Link to home
Start Free TrialLog in
Avatar of rocky050371
rocky050371

asked on

Mutex - Singleton Help

am using the following piece of code to ensure that my application only runs once, however I have a few questions about it.

      static Mutex m_Mutex; << in c# I assume that when the methods are static, so are the private members.

      public static void Run(Form mainForm)
      {
         if(IsFirstInstance())
         {
            Application.ApplicationExit += new EventHandler(OnExit);
            Application.Run(mainForm);
         }
      }
     
The following is causing me some confusion, when the application starts it creates a newly named Mutex. It then calls WaitOne and determines whether it receives any messages, if it does it returns true otherwise false. My questions are this, when the next application comes to create a new mutex with the same name, what exactly happens? Does it deny it therefore this is why the WaitOne does not receive any messages, also the timespan is set to zero, how can the mutex determine if any messages have been recieved in zero time?

      static bool IsFirstInstance()
      {
         m_Mutex = new Mutex(false,"SingletonApp Mutext");
         bool owned = false;
         owned = m_Mutex.WaitOne(TimeSpan.Zero,false);
         return owned ;
      }
Avatar of Fahad Mukhtar
Fahad Mukhtar
Flag of Pakistan image

I have given a solution to a similar problem here:
https://www.experts-exchange.com/questions/20633466/C-Disallow-multi-instances-and-display-the-BrowseForFolder-dialog.html#8628426

unfortunately i cant test your code as i dont have VS.Net right now, you can find TAD's comment very useful for using the singleton way to avoid the multiple instanced of the same application and yes private members can also be static

You need to use this Mutex constructor:

bool createdNew;
m_Mutex = new Mutex(true,"SingletonApp Mutext", out createdNew);

now, if createdNew is true, you created the mutex and own it; if it is false, the mutex already existed. No need to do a WaitOne() for this situation.
Avatar of rocky050371
rocky050371

ASKER

Thanks, but I was actually after an explanation of exactly what happens, then I may possibly choose an alternate root
Well, what happens is that the second app will not be able to get the mutex ownership. And that's what this flag which is returned does indicate.
Yes, but it is the WaitOne with a Zero Time Period which is slightly confusing.
ASKER CERTIFIED SOLUTION
Avatar of AvonWyss
AvonWyss
Flag of Switzerland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank you
Rocky, what would you have expected to give an A grade? What were my answers missing?
Perhaps a more detailed description.
Ok, thank you for your feedback.
Q. Would m_Mutex.Release need to be implemented as well?
I don't understand your question. The mutex has to remain allocated for the duration of the application, otherwise a second instance would not see the mutex as being in use. When the process is closed, the mutex will be released automatically. Therefore, while it may be good practice to release the mutex upon application exit, you must no do this earlier or the second instance detection will fail.
Automatically, I get it, thanks