【发布时间】:2019-03-13 16:25:19
【问题描述】:
大家好,我正在尝试从 Task.Run 中获得一些结果,但我无法弄清楚如何
我有多种方法希望并行运行并提取结果: 这是其中一种方法
protected override async Task<IList<EducationDTO>> GetEmployeesEducation(int userId)
{
IList<EducationDTO> userEducation = await EducationService.GetEducationsByUserId(userId);
return userEducation.Count > 0 ? userEducation : null;
}
这里所有方法都将并行运行
public async Task<DTOs.EmployeeDTO> GetEmployeeInfo(int userId)
{
EmployeeDTO employee = new EmployeeDTO();
Task task = Task.Run(() => {
Parallel.Invoke(
async () => { await GetEmployeeLanguages(userId); },
// ...
});
task.Wait();
/// extract result and process how ???
return employee;
}
【问题讨论】:
-
为什么不直接打电话给
var result = await EducationService.GetEducationsByUserId(userId);? -
@bash.d 他的
GetEmployeesEducation方法不是异步的。虽然它可能应该是。 -
@ThePerplexedOne 谢谢,没看到。但我认为这样做是有意义的。
-
嗨,是的,我可以这样做,但我选择不这样做,此时我只想看看不同的方法比较性能等
-
这种事情的性能可以忽略不计,即使你有几个不必要的电话,你没有。你可以允许
async向上传播。
标签: c# .net asynchronous async-await task-parallel-library