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

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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
 
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
 
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");

}

}
 
ano said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top