簡體   English   中英

C#IAsyncResult WaitAll

[英]C# IAsyncResult WaitAll

在WaitAll的一些實現中,我看到了以下代碼

IAsyncResult result1 = Method.BeginInvoke(10, MyCallback, null)
IAsyncResult result2 = Method.BeginInvoke(20, MyCallback, null)
WaitHandle[] waitHandles = new WaitHandle[] { result1.AsyncWaitHandle, result2.AsyncWaitHandle};
WaitHandle.WaitAll(waitHandles)

這看起來正確嗎? 在創建waitHandles數組之前,調用之一完成的機會是多少?

此致Dhananjay

我感覺合理。

// Begin invoking the first and second method, and give them a callback
IAsyncResult result1 = Method.BeginInvoke(10, MyCallback, null)
IAsyncResult result2 = Method.BeginInvoke(20, MyCallback, null)

// Any time past the point of invokation, MyCallback could be called.
// Get the wait handles of the async results, regardless of whether they have finished or not
WaitHandle[] waitHandles = new WaitHandle[] { result1.AsyncWaitHandle, result2.AsyncWaitHandle};

// Make sure to wait until the methods have finished.
// They could have finished immediately, and MyCallback could have already been called,
// but we will never get past this line unless they have finished.
WaitHandle.WaitAll(waitHandles)
// We may have waited on the function to finish, or they may have been done before we arrived at the previous line. Either way, they are done now.

您究竟發現什么奇怪?

問“什么是機會”是一個不好的信號。 可能會發生這種情況 ,這意味着在WaitAll之前以及方法完成時,您需要考慮程序需要執行的操作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM