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.
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.
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.
ManualResetEvent is thread safe. All its instance members are thread safe, therefore you do not have perform any thread synchronization.
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.
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); } }
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With