How to check if the WaitHandle was set?
Asked Answered
A

4

45

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!

Absolutely answered 22/7, 2010 at 20:24 Comment(1)
My answer was only seconds before SwDevMan's which is much clearer and includes the documentation quote, so I'm deleting it. However, I still wonder "Are you dealing with an auto-reset event that might already have been reset, or that the test code must not reset?"Switchback
F
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.

Ferminafermion answered 22/7, 2010 at 20:27 Comment(5)
+1 Good clear answer. Nice and simple, though I would suggest changing "Try" to "Use". As it stands, you seem unsure of your answer.Flabby
The only issue is that for some WaitHandles (auto-reset event, semaphore), the ready state will actually be reset by waiting on it.Switchback
@Vivin joy - millisecondsTimeout is the parameter name to the WaitOne call, please see the link. Its a quote taken from the Remarks sectionFerminafermion
@MartinVseticka Don't be ashamed! I came here with the same question. The answer is perhaps guessable, but by no means self-evident.Ohaus
It doesn't just check if the wait handle is AutoResetEvent! It actually changes its state to true.Montague
O
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

Olfactory answered 22/7, 2010 at 20:30 Comment(6)
Why use a keyword for the variable? It's unnecessary and makes your answer harder to read.Flabby
@Jeff Sure, it's just a hard thing to name for some sample code, and I've been writing a lot of code today.Olfactory
@chibacity: it's easy to write something other than event. waitHandle, resetEvent, mre, myEvent. The list is endless and it would make your example better.Flabby
@Jeff You have no idea of my state of mental exhaustion at the moment - but I will comply! :)Olfactory
@chibacity: I can sympathise but if a job's worth doing, it's worth doing well. +1Flabby
@Jeff Cheers for the kick up the bum, you are quite right. :)Olfactory
F
2

Use one of the Wait... methods on WaitHandle that takes a timeout value, such as WaitOne, and pass a timeout of 0.

Flabby answered 22/7, 2010 at 20:28 Comment(0)
C
1

You can use the WaitOne(int millisecondsTimeout, bool exitContext) method and pass in 0 for the timespan. It will return right away.

bool isSet = yourWaitHandle.WaitOne(0, true);
Conant answered 22/7, 2010 at 20:28 Comment(2)
Why should they use the one that takes an exitContext value? Considering that there are alternatives which do not require this field, you should explain its necessity.Flabby
Online help for VS2005 only shows WaitOne(), WaitOne(int,bool), and WaitOne(TimeSpan,bool). So, it's likely they didn't find WaitOne(int)Tattan

© 2022 - 2024 — McMap. All rights reserved.