Consider the following pattern:
private AutoResetEvent signal = new AutoResetEvent(false);
private void Work()
{
while (true)
{
Thread.Sleep(5000);
signal.Set();
//has a waiting thread definitely been signaled by now?
signal.Reset();
}
}
public void WaitForNextEvent()
{
signal.WaitOne();
}
The purpose of this pattern is to allow external consumers to wait for a certain event (e.g. - a message arriving). WaitForNextEvent
is not called from within the class.
To give an example that should be familiar, consider System.Diagnostics.Process
. It exposes an Exited
event, but it also exposes a WaitForExit
method, which allows the caller to wait synchronously until the process exits. this is what I am trying to achieve here.
The reason I need signal.Reset()
is that if a thread calls WaitForNextEvent
after signal.Set()
has already been called (or in other words, if .Set
was called when no threads were waiting), it returns immediately, as the event has already been previously signaled.
The question
- Is it guaranteed that a thread calling
WaitForNextEvent()
will be signaled beforesignal.Reset()
is called? If not, what are other solutions for implementing aWaitFor
method?
Instead of using
AutoResetEvent
orManualResetEvent
, use this:Then change your code like so: