Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / November 2006

Tip: Looking for answers? Try searching our database.

Help with Mutex

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Brian  Hampson - 15 Nov 2006 17:05 GMT
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 - 15 Nov 2006 17:10 GMT
> I'm new to getting multi-threading working in C#, so I'm looking for
> some help with Mutexes.
[quoted text clipped - 23 lines]
> 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
 }
}
///

Signature

Hope this helps,
Tom Spink

Google first, ask later.

Brian  Hampson - 15 Nov 2006 18:43 GMT
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:
>
[quoted text clipped - 17 lines]
> }
> ///
Jon Skeet [C# MVP] - 15 Nov 2006 19:01 GMT
> 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.)

Signature

Jon Skeet - <skeet@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 - 15 Nov 2006 19:24 GMT
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:
> > I'm new to getting multi-threading working in C#, so I'm looking for
> > some help with Mutexes.
[quoted text clipped - 16 lines]
> http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
> If replying to the group, please do not mail me too
Jon Skeet [C# MVP] - 15 Nov 2006 22:38 GMT
> 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
[quoted text clipped - 4 lines]
> 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.

Signature

Jon Skeet - <skeet@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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.