本文介绍了等待不同类型的任务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

IEnumerable<Task<Request>> requestTasks = CreateRequestTasks();
Task<Trace> traceTask = CreateTraceTask();

var tasks = new List<Task>();
tasks.AddRange(requestTasks);
tasks.Add(traceTask);

await Task.WhenAll(tasks);

如何从requestTasks集合中获取结果?

How do I get the result from the requestTasks collection?

推荐答案

将其保留为一个单独的(已归类的)集合:

Keep it as a separate (reified) collection:

List<Task<Request>> requestTasks = CreateRequestTasks().ToList();
...
await Task.WhenAll(tasks);
var results = await Task.WhenAll(requestTasks);

请注意,第二个await Task.WhenAll实际上不会执行任何异步等待",因为所有这些任务已经完成.

Note that the second await Task.WhenAll won't actually do any "asynchronous waiting", because all those tasks are already completed.

这篇关于等待不同类型的任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-03 13:08