473,326 Members | 2,114 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,326 software developers and data experts.

Help with Mutex

I'm new to getting multi-threading working in C#, so I'm looking for
some help with Mutexes.
I have code that looks like the following:

Mutex MyMutex = new Mutex();

Then later in parts that I want to make sure work at separate times:

void Part1()
{
MyMutex.WaitOne();
{//Do Stuff}
MyMutex.ReleaseMutex();
}

void Part2()
{
myTimer.Stop();
MyMutex.WaitOne();
{//Do other stuff that must be not at the saame time as Part1}
MyMutex.ReleaseMutex();
myTimer.Start();
}

Should this work? Part1 is triggered via a FileSystemWatcher and Part2
is triggered via a timer tick.

Nov 15 '06 #1
5 3144
Brian Hampson wrote:
I'm new to getting multi-threading working in C#, so I'm looking for
some help with Mutexes.
I have code that looks like the following:

Mutex MyMutex = new Mutex();

Then later in parts that I want to make sure work at separate times:

void Part1()
{
MyMutex.WaitOne();
{//Do Stuff}
MyMutex.ReleaseMutex();
}

void Part2()
{
myTimer.Stop();
MyMutex.WaitOne();
{//Do other stuff that must be not at the saame time as Part1}
MyMutex.ReleaseMutex();
myTimer.Start();
}

Should this work? Part1 is triggered via a FileSystemWatcher and Part2
is triggered via a timer tick.
Hi Brian,

Possibly. But what happens if an error occurs somewhere, and your mutex
isn't released?

The best way, IMHO, to do this would be to use locking:

///
Object lockObject = new Object();

void Part1()
{
lock ( lockObject )
{
// Do Stuff
}
}

void Part2()
{
lock ( lockObject )
{
// Do Other Stuff
}
}
///

--
Hope this helps,
Tom Spink

Google first, ask later.
Nov 15 '06 #2
Thanks,

I'll give it a try. I'm still not sure why my Mutex code wasn't
working as hoped, I had try/catch/finally inside those mutex locks.
Meh. I'll see about the lock.

Brian Hampson
System Administrator
ALS Laboratory Group, North America
The best way, IMHO, to do this would be to use locking:

///
Object lockObject = new Object();

void Part1()
{
lock ( lockObject )
{
// Do Stuff
}
}

void Part2()
{
lock ( lockObject )
{
// Do Other Stuff
}
}
///

--
Hope this helps,
Tom Spink

Google first, ask later.
Nov 15 '06 #3
Brian Hampson <br***********@gmail.comwrote:
I'm new to getting multi-threading working in C#, so I'm looking for
some help with Mutexes.

I have code that looks like the following:
Code "like" what's pasted is rarely useful - it's easy to miss the bugs
out at the same time as stripping other things (like try/finally).

Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.

(I agree with Brian though - using lock is a lot better.)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Nov 15 '06 #4
Well, I've gone to the lock for now, and when I was stripping all the
comments out of the original function, I found that I had a return that
wouldn't release the mutex in certain circumstances. Doh!

A Homer moment :(

In THEORY, however (if I hadn't had that leaking MUTEX issue)... was my
concept of Mutex implementation sound?

Brian Hampson

Jon wrote:
Brian Hampson <br***********@gmail.comwrote:
I'm new to getting multi-threading working in C#, so I'm looking for
some help with Mutexes.

I have code that looks like the following:

Code "like" what's pasted is rarely useful - it's easy to miss the bugs
out at the same time as stripping other things (like try/finally).

Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.

(I agree with Brian though - using lock is a lot better.)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Nov 15 '06 #5
Brian Hampson <br***********@gmail.comwrote:
Well, I've gone to the lock for now, and when I was stripping all the
comments out of the original function, I found that I had a return that
wouldn't release the mutex in certain circumstances. Doh!

A Homer moment :(

In THEORY, however (if I hadn't had that leaking MUTEX issue)... was my
concept of Mutex implementation sound?
Well, the theory of "wait for the mutex, then release it" is sound - so
long as you don't have multiple mutexes and obtain them in different
orders, etc.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Nov 15 '06 #6

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

Similar topics

1
by: Didier FRAISSE | last post by:
i want to be sure that only one instance of my script is running at the same time i try this little script #--------------------------------------------------------------------------- # -*-...
0
by: Srijit Kumar Bhadra | last post by:
Hello, Here is some sample code with pywin32 build 203 and ctypes 0.9.6. Best regards, /Srijit File: SharedMemCreate_Mutex_win32all.py # This application should be used with...
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...
193
by: Michael B. | last post by:
I was just thinking about this, specifically wondering if there's any features that the C specification currently lacks, and which may be included in some future standardization. Of course, I...
16
by: Ed Sutton | last post by:
I use a mutex to disallow starting a second application instance. This did not work in a release build until I made it static member of my MainForm class. In a debug build, first instance got...
11
by: mwt | last post by:
Hi. I'm reworking a little app I wrote, in order to separate the data from the UI. As a start, I wanted to create a iron-clad data recepticle that will hold all the important values, and stand up...
1
by: anadiks | last post by:
Problem 2: Threading: Requirements: In a particular system there are two threads T1 and T2. T1 is the master thread that periodically sends messages to the worker thread T2. The system...
3
by: NaeiKinDus | last post by:
Hello, i'm trying to program a thread that would be locked (by a mutex) and that would only be unlocked once that a function (generating data) is done. The purpose is to generate data, and unlock...
11
by: Lamont Sanford | last post by:
Given an object of type Mutex, what method or property should be called to determine if it is currently owned? I could call WaitOne(0,false) -- and if the return value is false, I could deduce...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.