473,385 Members | 1,893 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,385 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 2219
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.