我知道如何使用 ManualResetEvent 或同步原语(如 Monitor)来等待事件和/或锁,但我想知道是否有一种方法可以实现以下内容:
ManualResetEvent resetEvent;
public string WaitForOneThousandMs()
{
resetEvent.Wait(1000);
if (WaitTime(resetEvent) <= 1000)
return "Event occured within 1000ms.";
else
return "Event did not occur within 1000ms.";
}
1) 等待 1000ms 让事件 X 发生
2) 如果事件发生在 1000ms 内,执行路径 A
3)否则,执行路径B
这基本上是一个条件等待函数,条件是我们必须等待多长时间,如果可能的话,实现它的最佳方法是什么?