[英]Executing parallel tasks -> wait for all -> make use of result
GetSystems()
and GetActions()
both returns IEnumerable<T>
of different types. GetSystems()
和GetActions()
都返回不同类型的IEnumerable<T>
。 What do I need to do to access the results below? 访问以下结果我需要做什么? Do I need to make use of Task.WaitAll()
or something similar? 我是否需要使用Task.WaitAll()
或类似的东西?
Task t1 = new Task(() => GetSystems());
Task t2 = new Task(() => GetActions());
List<Task> tasks = new List<Task>() { t1, t2 };
Parallel.ForEach(tasks, t =>
{
t.Start();
});
//t1.Result...?
I'm using C# 4.0. 我正在使用C#4.0。
Update: 更新:
private Task<List<SYSTEM>> GetSystems()
{
return Task.Factory.StartNew(() =>
{
using (var context = new DbContext())
{
return context.SYSTEM.ToList();
}
});
}
You need to use Task<T>
in order to use the Result
property. 您需要使用Task<T>
才能使用Result
属性。 Given the comments, it looks like you want: 鉴于评论,它看起来像你想要:
Task<List<SYSTEM>> t1 = ...;
Task<List<ACTION>> t2 = ...;
Task[] tasks = { t1, t2 };
Parallel.ForEach(tasks, t => t.Start());
Task.WaitAll(tasks);
List<SYSTEM> systems = t1.Result;
List<ACTION> actions = t2.Result;
You should consider how you'll handle failure cases though. 你应该考虑如何处理失败案例。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.