473,507 Members | 2,374 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

threading & mutex: question about example

Hi all,

I've been trying to get my head around threading. Here's an example
from the book I'm reading:

/***************************/
Mutex m = null;
const string name = "xyz";

try
{
m = Mutex.OLpenExisting(name);
}
catch (WaitHandleCannotBeOpenedException)
{ }

if (m == null)
{
// POINT 'A'
m = new Mutex(true, name);
}
else
{
m.close();
return;
}
/*****************************/

This example can be used in the main method to prevent two instances
of the same application to be run.

Now, as a newbie to threading, it seems to me that theoretically two
threads might *both* get to "Point 'A'", correct?

Thanks, regards,
Jeroen
Jan 7 '08 #1
2 2046
Jeroen,

Yes, two threads might get to point 'A', but that's not really the
problem here. Because the Mutex is named, it is visible to other processes,
so you have to worry about another process opening the thread if it finds
out that it doesn't exist.

To be honest, this isn't the best way to open or create a Mutex which is
named, as you have this race condition.

It's a better idea to call the CreateMutex API function through the
P/Invoke layer, which will open or create the named mutex if it doesn't
exist. Then, with the handle returned from that function, you would set the
SafeWaitHandle property of a new Mutex instance to that handle and call the
appropriate methods.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Jeroen" <me******@gmail.comwrote in message
news:91**********************************@q39g2000 hsf.googlegroups.com...
Hi all,

I've been trying to get my head around threading. Here's an example
from the book I'm reading:

/***************************/
Mutex m = null;
const string name = "xyz";

try
{
m = Mutex.OLpenExisting(name);
}
catch (WaitHandleCannotBeOpenedException)
{ }

if (m == null)
{
// POINT 'A'
m = new Mutex(true, name);
}
else
{
m.close();
return;
}
/*****************************/

This example can be used in the main method to prevent two instances
of the same application to be run.

Now, as a newbie to threading, it seems to me that theoretically two
threads might *both* get to "Point 'A'", correct?

Thanks, regards,
Jeroen

Jan 7 '08 #2
Nicholas,

Thanks for the quick response. I understand your answer (for the
larger part), though I'm a bit confused on the term "P/Invoke layer".
However, that seemed a different topic altogether so I posted a new
message in the newsgroup here:

http://groups.google.nl/group/micros...3883d5cdd010e9

Thanks again for the mutex explanation.

Regards,
Jeroen
Jan 7 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
4079
by: Bardo | last post by:
Hi, I have a situation where I am capturing both a WMI event utilising the "ManagementEventWatcher" in the "System.Management" namespace, and a corresponding event ("EntryWritten") raised from...
3
1123
by: Steve B. | last post by:
Request comments on the code that should be wriiten: 1. Need users to start a application from local network but only allow one instance of the application to run on EACH users machine while, at...
16
351
by: One Handed Man \( OHM - Terry Burns \) | last post by:
Sorry if this gets duplicated, but I posted it and cant see it for a long time so repost. . . I have an application which is writing to a graphics object, the main UI thread and a worker thread...
17
1424
by: One Handed Man \( OHM - Terry Burns \) | last post by:
Assumes a Form with a Panel on it., Does the Mutex have to be within the address of a thread start address ? Cheers - OHM '----------- *************** ---------------- Private...
2
1522
by: WXS | last post by:
When I see things in .NET 2.0 like obsoletion of suspend/resume because of the public reason MS gives of they think people are using them inappropriately.. use mutex, monitor and other...
6
1307
by: MobileBoy36 | last post by:
Hi All, I want to make a LogFile class that is thread safe. I use a Mutex for it. But the behavior of the class is not that normal. In a c# guide I read you can achieve it by simply using...
9
1836
by: cgwalters | last post by:
Hi, I've recently been working on an application which does quite a bit of searching through large data structures and string matching, and I was thinking that it would help to put some of this...
126
6609
by: Dann Corbit | last post by:
Rather than create a new way of doing things: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2497.html why not just pick up ACE into the existing standard:...
7
6124
by: dmitrey | last post by:
hi all, Is there a better way to kill threading.Thread (running) instance than this one http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/496960 (it's all I have found via google). BTW,...
0
7223
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
7111
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
7376
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
4702
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3191
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3179
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1542
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
760
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
412
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.