检查 AutoResetEvent 状态。

10

如何检查 AutoResetEvent 对象是通过超时还是通过从另一方法调用 Set() 触发的?

这是我的代码:

private AutoResetEvent autoResetEvent = new AutoResetEvent(false);
private int timeout = 30000;

public void SyncMethod()
{
    // some code before
    autoResetEvent.WaitOne(timeout);
    // if autoResetEvent called by timeout then { do some stuff } 
    // some code after
}
public void AsyncMethod()
{
    // some code before
    // ok I am done
    autoResetEvent.Set();
}
2个回答

15

WaitHandle::WaitOne 方法 (Int32)

返回值类型:System::Boolean

如果当前实例接收到信号,则返回 true;否则返回 false。

因此,在超时后会返回 false。


3

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接