2012-03-30 37 views

回答

5

由於.NET 4.5可以消除WaitHandleCannotBeOpenedException例外情況下,當一個名爲通過使用TryOpenExisting()法系統事件不存在:

EventWaitHandle result = null; 
if (!EventWaitHandle.TryOpenExisting("eventName", out result)) 
{ 
    if (result == null) 
    { 
     // event was not found 
    }else 
    { 
     // result represent a cross process WaitEvent handle 
    } 
} 

public static bool TryOpenExisting(
         string name, 
         out EventWaitHandle result 
) 

MSDN

如果您不確定是否存在已命名的同步事件,請使用 此方法過載而不是OpenExisting方法過載, 如果同步事件不存在則拋出異常

相關問題