2012-07-16 157 views
8

是否可以檢查AutoResetEvent實際對象的處理方式?它是通過超時還是通過從另一個方法調用Set()來觸發的?檢查AutoResetEvent狀態

這是我的代碼。

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

回答

12

WaitHandle的:: WaitOne的方法(Int32)

返回值類型:系統::布爾

如果當前實例接收信號真;否則,是錯誤的。

所以,超時後返回false。

+0

太容易了。 :) 謝謝! – 2012-07-17 08:13:08