超时后调用了AutoResetEvent集

本文关键字:AutoResetEvent 调用 超时 | 更新日期: 2023-09-27 18:22:37

来自MSDN"如果没有等待线程,等待句柄将保持信号状态,直到线程尝试等待它,或者直到调用它的Reset方法。"

EventWaitHandle MyWaitHandle = new AutoResetEvent(false);

线程#1:

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

线程#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();
}

在我的应用程序中,线程#1正在向外部应用程序提交请求,并等待信号或超时。如果从外部应用程序接收到响应,线程#2将设置等待句柄。即使在超时之后也可以调用该set。我的问题是

1) 在导致设置信号的超时之后可以调用CCD_ 3的可能性很高。这是否意味着,无论何时将来有对线程#1的请求,WaitOne(10000)都不会生效,将立即发布?

2) 如果超时,我是否可以不调用Method2中的set?这会给线程#1带来任何问题吗?

超时后调用了AutoResetEvent集

为什么不在等待之前确保等待句柄始终重置?

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
}