Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find what state ManualResetEvent is in?

I am using an instance of ManualResetEvent to control thread access to a resource but I'm running into problems with it. Does anyone know how I can find out during debugging what the state of the object is?

That is to say I would like to know if the ManualResetEvent is currently blocking any threads and maybe even how many and which threads it is blocking.

like image 326
George Mauer Avatar asked Dec 23 '08 15:12

George Mauer


People also ask

What is ManualResetEvent in C#?

Set to signal that the waiting threads can proceed. All waiting threads are released. Once it has been signaled, ManualResetEvent remains signaled until it is manually reset by calling the Reset() method. That is, calls to WaitOne return immediately.

How do I use ManualResetEvent?

Start the main thread; When an asynchronous worker thread is triggered, call the ManualResetEvent object's WaitOne() method to block the main thread; When the worker thread has completed, call the ManualResetEvent object's Set() method to release the main thread and allow it to continue.

Is ManualResetEvent thread safe?

ManualResetEvent is thread safe. All its instance members are thread safe, therefore you do not have perform any thread synchronization.


3 Answers

Perform a WaitOne on the event with a timeout value of zero.

It will return true if the event is set, or false if the timeout occurs. In other words, true -> event is set, false -> event is not set.

like image 52
Andrew Rollings Avatar answered Oct 11 '22 07:10

Andrew Rollings


Here is working code:

private ManualResetEvent pause = new ManualResetEvent(false);
pause.WaitOne(); // caller thread pauses
pause.Set();    // another thread releases paused thread

// Check pause state
public bool IsPaused { get { return !pause.WaitOne(0); } }
like image 45
fab Avatar answered Oct 11 '22 08:10

fab


You can make function calls in the Debugger Watch window. Add a call to mreVariable.WaitOne(0) in the Watch window and see what it evaluates to. Note: You should not use this for AutoResetEvents since that could change the actual state.

like image 1
Robert Biro Avatar answered Oct 11 '22 09:10

Robert Biro