我有几个任务,每个任务都有一个使用任务结果的 ContinueWith
。类似这样的事情:
Task myTask01 = myMethod01Async().ContinueWith((a) => //do somenthing with
a.result);
Task myTask02 = myMethod02Async().ContinueWith((a) => //do somenthing with a.result);
Task.WhenAll(myTask01, myTask02);
我知道 WhenAll 会等待参数中的所有任务完成。但在这种情况下,我有一个 ContinueWith
,我不知道 whenAll 是否会等到所有 ContinueWith
完成或在 Task01
和 Task02
完成,因此尽管 ContinueWith
代码仍在运行,但代码仍在运行。
最佳答案
ContinueWith返回一个新任务,因此通过使用 Task. WhenAll,您实际上正在等待从 ContinueWith
返回的任务,而不是从 myMethod01Async
和 myMethod02Async
返回的任务。
所以是的,任务。 WhenAll 将等待 ContinueWith
内的代码完成。
关于c# - 对于任务,有 continueWith 和 WhenAll(),什么时候继续,什么时候完成任务,什么时候完成 ConitueWith?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45779451/