Mutex - Singleton Help

  • Thread starter Thread starter Rocky
  • Start date Start date
R

Rocky

I 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 ;
}
 
Back
Top