检查AutoResetEvent状态

Pav*_*ykh 8 .net multithreading autoresetevent

是否有可能检查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();
}
Run Code Online (Sandbox Code Playgroud)

Ale*_*x F 13

WaitHandle :: WaitOne方法(Int32)

返回值类型:System :: Boolean

如果当前实例收到信号,则为true; 否则,错误.

因此,超时后返回false.