首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >C# IAsyncResult WaitAll

C# IAsyncResult WaitAll
EN

Stack Overflow用户
提问于 2011-04-12 10:40:10
回答 1查看 4.6K关注 0票数 1

在WaitAll的一些实现中,我看到了以下代码

代码语言:javascript
复制
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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-04-12 10:47:36

对我来说很有意义。

代码语言:javascript
复制
// 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之前完成,那么您需要考虑程序需要做什么。

票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5629811

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档