对于任务,continueWith 和 WhenAll(),什么时候继续,什么时候完成任务或者什么时候完成 ConitueWith?

With tasks, continueWith and WhenAll(), when continue, when finish task or when finish ConitueWith?

我有几个任务,每个任务都有一个 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 完成还是在 Task01Task02 完成时继续,所以代码继续,尽管 ContinueWith 代码仍然是 运行。

ContinueWith returns 一个新任务,所以通过使用 Task. WhenAll,您实际上是在等待从 ContinueWith 而不是 myMethod01AsyncmyMethod02Async.

返回的任务

是的,任务。 WhenAll 将等待 ContinueWith 中的代码完成。