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.ApplicationExi t += 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.Z ero,false) ;
return owned ;
}
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.ApplicationExi
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.Z
return owned ;
}
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.
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.
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.
ASKER
Yes, but it is the WaitOne with a Zero Time Period which is slightly confusing.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank you
Rocky, what would you have expected to give an A grade? What were my answers missing?
ASKER
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
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