Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Help with Mutex

1 view
Skip to first unread message

Brian Hampson

unread,
Nov 15, 2006, 12:05:28 PM11/15/06
to
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.

Tom Spink

unread,
Nov 15, 2006, 12:10:30 PM11/15/06
to
Brian Hampson wrote:

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.

Brian Hampson

unread,
Nov 15, 2006, 1:43:00 PM11/15/06
to
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

Jon Skeet [C# MVP]

unread,
Nov 15, 2006, 2:01:26 PM11/15/06
to
Brian Hampson <brian....@gmail.com> 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:

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

Brian Hampson

unread,
Nov 15, 2006, 2:24:04 PM11/15/06
to
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 Skeet [C# MVP]

unread,
Nov 15, 2006, 5:38:59 PM11/15/06
to
Brian Hampson <brian....@gmail.com> wrote:
> 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.

0 new messages