使用任务,continueWith和WhenAll(),何时继续,完成任务或完成ConitueWith?

时间:2017-08-20 07:12:51

标签: c# asynchronous task-parallel-library

我有几个任务,每个人都有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,我不知道当所有ContinueWith完成时是等待还是完成,或者当Task01Task02完成时是否继续,所以代码继续虽然ContinueWith代码仍在运行。

1 个答案:

答案 0 :(得分:2)

ContinueWith返回一个新任务,因此使用Task。 WhenAll,您实际上正在等待从ContinueWith而不是myMethod01AsyncmyMethod02Async返回的任务。

所以是的,任务。 WhenAll将等待ContinueWith内的代码完成。

相关问题