473,394 Members | 1,693 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

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 2220
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Ken Varn | last post by:
I have a named mutex object that is accessed by both an asp.net application and a Windows executable .net application. The Windows executable runs under the administrator logon, while the asp.net...
1
by: Rocky | last post by:
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...
3
by: g | last post by:
hello! here is some code: #ifndef RESOURCE_H_ #define RESOURCE_H_ #include <boost/date_time/gregorian/gregorian.hpp> #include <boost/thread/mutex.hpp>
6
by: roman.muntyanu | last post by:
Hi All, I have a program that can be started as windows service or as an windows app. I need to avoid starting this program as an application if it is already started as a win service. In...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.