gpt4 book ai didi

C# IAsyncResult 等待所有

转载 作者:太空宇宙 更新时间:2023-11-03 19:29:59 24 4
gpt4 key购买 nike

在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 数组之前,其中一个调用完成的可能性有多大?

问候,达南杰

最佳答案

对我来说很有意义。

// 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 之前以及何时完成方法需要做什么。

关于C# IAsyncResult 等待所有,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5629811/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com