1

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?

user1178376
  • 928
  • 2
  • 10
  • 24
  • Why would you not want to set the event even after a timeout? Does Method2 not signal that something is ready for Method1? Perhaps if you explain what you're trying to accomplish... – 500 - Internal Server Error Feb 06 '12 at 18:28
  • `Method2()` would not know if it is being called after timeout. There lies the issue. Well, I can monitor the time and set a `bool` variable to find out if it is timedout. But Can I call the `Set()` based on a `bool` value. If I don't call would it leave any open handles or anything? – user1178376 Feb 06 '12 at 19:44

1 Answers1

2

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
}
Chris Shain
  • 50,833
  • 6
  • 93
  • 125