C#的IAsyncResult为WaitAll [英] C# IAsyncResult WaitAll

查看:190
本文介绍了C#的IAsyncResult为WaitAll的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在一些我看到了下面的代码

In some of the implementations of WaitAll I have seen the following code

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阵列的调用完成?

Does this seem right ? What are the chances that before the waitHandles array is created one of the calls complete ?

问候,
Dhananjay

Regards, 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.



你到底觉得奇怪?

What exactly do you find odd?

问什么是机会,是一个不好的迹象。机会是的可能会发生的,这意味着你将需要考虑程序需要做的,如果当你完成之前方法为WaitAll。

Asking "what are the chances" is a bad sign. The chances are it might happen which means you will need to account for what the program needs to do if and when the methods complete before you WaitAll.

这篇关于C#的IAsyncResult为WaitAll的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆