469,927 Members | 1,242 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,927 developers. It's quick & easy.

Mutex - Singleton Help

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 ;
}


Jul 21 '05 #1
1 2063
pm
Here is a page that will answer your some if not all of your questions. It
also has an alternative approach to what you are doing.

http://www.ai.uga.edu/~mc/SingleInstance.html

"Rocky" wrote:
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 ;
}


Jul 21 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Rocky | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.