Question

From MSDN "If there are no waiting threads, the wait handle remains signaled until a thread attempts to wait on it, or until its Reset method is called."

EventWaitHandle MyWaitHandle = new AutoResetEvent(false);

Thread # 1:

public void Method1()
{
  //do something
  //wait for the signal or timeout
  MyWaitHandle.WaitOne(10000);
  //do something else on receiving signal or after timeout
}

Thread # 2:

//this would be called when there is a response from the external app
public void Method2()
{
  //do something
  //send the signal to waiting thread
  MyWaitHandle.Set();
}

In my application Thread # 1 is submitting a request to external app and waiting for a signal or timeout. If a response is received from the external app Thread # 2 sets the wait handle. This set can be called even after the timeout. My questions are

1) It is highly possible that Method2 can be called after the timeout resulting in setting the signal. Does that mean whenever there is a request to Thread # 1 in the future, the WaitOne(10000) has no effect and will be released immediately?

2) Is there anyway for me to not call set in Method2 in case of timeout? Would that cause any problems to the Thread # 1?

Was it helpful?

Solution

Why not just make sure the wait handle is always reset before waiting on it?

public void Method1()
{
  // Reset the wait handle I'll be using...
  MyWaitHandle.Reset();

  //do something
  //wait for the signal or timeout
  MyWaitHandle.WaitOne(10000);
  //do something else on receiving signal or after timeout
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top