How to find what state ManualResetEvent is in?

.NetMultithreadingLocking

.Net Problem Overview


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.

.Net Solutions


Solution 1 - .Net

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.

Solution 2 - .Net

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

Solution 3 - .Net

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionGeorge MauerView Question on Stackoverflow
Solution 1 - .NetAndrew RollingsView Answer on Stackoverflow
Solution 2 - .NetfabView Answer on Stackoverflow
Solution 3 - .NetRobert BiroView Answer on Stackoverflow