Question

How to check if the WaitHandle was set?

I have a WaitHandle and I would like to know how to check if the WaitHandle has already been set or not.

Note: I can add a bool variable and whenever Set() method is used set the variable to true, but this behaviour must be built in WaitHandle somewhere.

Thanks for help!

 45  21657  45
1 Jan 1970

Solution

 62

Try WaitHandle.WaitOne(0)

If millisecondsTimeout is zero, the method does not block. It tests the state of the wait handle and returns immediately.

2010-07-22

Solution

 7
const int DoNotWait = 0;
                          
ManualResetEvent waitHandle = new ManualResetEvent(false);                   

Console.WriteLine("Is set:{0}", waitHandle.WaitOne(DoNotWait));
         
waitHandle.Set(); 

Console.WriteLine("Is set:{0}", waitHandle.WaitOne(DoNotWait));   

Output:

Is set:False

Is set:True

2010-07-22