By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,761 Members | 1,498 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,761 IT Pros & Developers. It's quick & easy.

Can't call Mutex.WaitOne() again after Mutex.ReleaseMutex()

P: n/a
ano
How to reset Mutex.ReleaseMutex()? I can't call Mutex.WaitOne() again because
the Mutex status seem to be fix to ReleaseMutex().

Do I need to create new Mutex() every time after ReleaseMutex()?

Thanks
Mar 31 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Hello, ano!

a> How to reset Mutex.ReleaseMutex()? I can't call Mutex.WaitOne() again because
a> the Mutex status seem to be fix to ReleaseMutex().

Why can't you call WaitOne() are there any exceptions thrown?

a> Do I need to create new Mutex() every time after ReleaseMutex()?
No

--
Regards, Vadym Stetsyak
www: http://vadmyst.blogspot.com
Mar 31 '06 #2

P: n/a
ano
> Why can't you call WaitOne() are there any exceptions thrown?
No, it just doesn't stop!

What's wrong?

class Class1
{
static Thread t1;
static Thread t2;
static Mutex m;

[STAThread]
static void Main(string[] args)
{
Class1 c = new Class1();
m = new Mutex(true);

t1 = new Thread( new ThreadStart(c.thread1) );
t2 = new Thread( new ThreadStart(c.thread2) );

t1.Start();
t2.Start();

Thread.Sleep(2000);

m.ReleaseMutex();

Console.Read();
}

public void thread1()
{
Console.WriteLine("Thread1 before wait");
m.WaitOne();
Console.WriteLine("Waitagain");

// something wrong here??
m.WaitOne();
Console.WriteLine("Should not come to here!!");
}

public void thread2()
{
for (int i=0; i<500; i++);

Console.WriteLine("Release");

}

}
Mar 31 '06 #3

P: n/a

ano wrote:
Why can't you call WaitOne() are there any exceptions thrown?

No, it just doesn't stop!


Calling Mutex.WaitOne will put the thread to sleep only if another
thread is holding the mutex (that is, anothrer thread has already
called Mutex.WiatOne and isn't blocked on this call).
In that situation, the call to WaitOne will return (ie, the thread will
be rescheduled for execution) once the previously holding thread has
called ReleaseMutex.

Otherwise, WaitOne will not block, and your thread will happily
continue it's execution, holding the mutex until you release it.

Arnaud
MVP - VC

Mar 31 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.