【问题标题】:With tasks, continueWith and WhenAll(), when continue, when finish task or when finish ConitueWith?对于任务,continueWith 和WhenAll(),何时继续,何时完成任务或何时完成 ConitueWith?
【发布时间】:2017-08-20 07:12:51
【问题描述】:

我有几个任务,每个任务都有一个使用任务结果的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 代码是仍在运行。

【问题讨论】:

  • “我不知道 whenAll 是等到所有 ContinueWith 都完成还是在 Task01 和 Task02 完成时继续” -- 任务 Task01Task02 正是ContinueWith()结束的表示。您的问题没有多大意义,因为您暗示它们是不同的,而实际上它们是相同的。当然,如果您只是阅读文档,甚至尝试一个示例,您就可以回答自己的问题。

标签: c# asynchronous task-parallel-library


【解决方案1】:

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

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

【讨论】:

猜你喜欢
  • 2015-09-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-04-08
  • 1970-01-01
相关资源
最近更新 更多