2010-07-18 40 views
0

提前感謝您的幫助。有沒有辦法取消「AsyncWaitHandle.WaitOne()」?

我想在我的代碼中取消AsyncWaitHandle.WaitOne()(這意味着我不想再阻止),但是我只是無法找到辦法。也許,這是我的程序的邏輯問題,但我一想到退出信號就想不出其他方式擺脫循環。以下是有問題的代碼:

s.Bind(endPoint); 
s.Listen(15); 
while (true) { 
    thrd = s.BeginAccept(new AsyncCallback(Accept_Callback), s); 
    thrd.AsyncWaitHandle.WaitOne(); // it still blocks here when next statement becomde "true" 
    if (_syncEvents.ExitThreadEvent.WaitOne(0, false)) 
    { 
     //I want to break out of the loop at this point, it breaks out only when next call coming 
     thrd.AsyncWaitHandle.Close(); 
     s.Close(); 
     break; 
    } 
} 

請幫忙,謝謝!

亞倫陳

回答

2

你可以使用手動重置事件:

ManualResetEvent evt = new ManualResetEvent(false); 

    WaitHandle[] handles = new WaitHandle[] { evt, thrd.AsyncWaitHandle }; 

    WaitHandle.WaitAny(handles); 

然後,從另一個線程,請致電:

evt.Set(); 

釋放插座。

+0

謝謝保羅凱斯特, 我會嘗試,讓你知道它是怎麼回事,謝謝! Aaron – AaronChen 2010-07-18 10:07:40

+0

Paul, 感謝您解決我的問題。 Aaron – AaronChen 2010-07-18 11:03:19

相關問題