November 11, 2008

Calling WaitOne With A TimeOut

... 
AutoResetEvent finishedEvent = null;
...
finishedEvent = new AutoResetEvent(false);
try
{
 DoSomethingOnAnotherThread()
 // Wait until the 'finishedEvent' is 'Set()'
 // to tell us that the job is complete
 if (finishedEvent.WaitOne(TWENTY_SECONDS, false))
 {
  OnSuccess();
 }
 else // Timed out before event was called
 {
  OnTimedOut();
 }
}
finally
{
 finishedEvent.Close();
 finishedEvent = null;
}        

No comments: