如何将所有等待 ViewModel 添加到 Task.WhenAll
How to add all awaits ViewModel to Task.WhenAll
如何在 Task.WhenAll() 中添加这三个等待,但使用我的模型的 return 值,因为如果不在 WhenAll() 中添加它,我可以获得 return 我的模型。但是当在 Task.WhenAll() 中添加它时,我得到的只是一些 AsyncState、id 属性。
var _pending = await _lookupService.GetFirstOrDefaultAsync<StatusLookup>(x => x.Code == LookupConstants.Pending);
var _cancel = await _lookupService.GetFirstOrDefaultAsync<StatusLookup>(x => x.Code == LookupConstants.Cancel);
var _completed = await _lookupService.GetFirstOrDefaultAsync<StatusLookup>(x => x.Code == LookupConstants.Completed);
List<Task> taskList = List<Task<StatusLookup>>();
taskList.Add(_lookupService.GetFirstOrDefaultAsync<StatusLookup>(x => x.Code == LookupConstants.Pending));
taskList.Add(_lookupService.GetFirstOrDefaultAsync<StatusLookup>(x => x.Code == LookupConstants.Cancel));
taskList.Add(_lookupService.GetFirstOrDefaultAsync<StatusLookup>(x => x.Code == LookupConstants.Completed));
var result = await Task.WhenAll(taskList);
谢谢
为了用尽可能少的语言告诉它,您应该将任务(没有 await
)分配给变量。然后用 WhenAll
等待他们,最后在 Result
属性.
中读取他们的结果
您可以在WhenAll
returns后阅读结果。您可以使用 await
(在完成的任务上再次调用它不会执行任何操作)或从 Result
.
中读取它
var pendingTask = _lookupService.GetFirstOrDefaultAsync<StatusLookup>(x => x.Code == LookupConstants.Pending);
var cancelTask = _lookupService.GetFirstOrDefaultAsync<StatusLookup>(x => x.Code == LookupConstants.Cancel);
var completedTask = _lookupService.GetFirstOrDefaultAsync<StatusLookup>(x => x.Code == LookupConstants.Completed);
await Task.WhenAll( new [] { pendingTask, cancelTask, completedTask } );
_pending = pendingTask.Result;
_cancel = cancelTask.Result;
_complete = completedTask.Result;
您甚至可以跳过 WhenAll
。这会给你一个稍微不同的行为,但它可能同样有效:
var pendingTask = _lookupService.GetFirstOrDefaultAsync<StatusLookup>(x => x.Code == LookupConstants.Pending);
var cancelTask = _lookupService.GetFirstOrDefaultAsync<StatusLookup>(x => x.Code == LookupConstants.Cancel);
var completedTask = _lookupService.GetFirstOrDefaultAsync<StatusLookup>(x => x.Code == LookupConstants.Completed);
_pending = await pendingTask;
_cancel = await cancelTask;
_complete = await completedTask;
如何在 Task.WhenAll() 中添加这三个等待,但使用我的模型的 return 值,因为如果不在 WhenAll() 中添加它,我可以获得 return 我的模型。但是当在 Task.WhenAll() 中添加它时,我得到的只是一些 AsyncState、id 属性。
var _pending = await _lookupService.GetFirstOrDefaultAsync<StatusLookup>(x => x.Code == LookupConstants.Pending);
var _cancel = await _lookupService.GetFirstOrDefaultAsync<StatusLookup>(x => x.Code == LookupConstants.Cancel);
var _completed = await _lookupService.GetFirstOrDefaultAsync<StatusLookup>(x => x.Code == LookupConstants.Completed);
List<Task> taskList = List<Task<StatusLookup>>();
taskList.Add(_lookupService.GetFirstOrDefaultAsync<StatusLookup>(x => x.Code == LookupConstants.Pending));
taskList.Add(_lookupService.GetFirstOrDefaultAsync<StatusLookup>(x => x.Code == LookupConstants.Cancel));
taskList.Add(_lookupService.GetFirstOrDefaultAsync<StatusLookup>(x => x.Code == LookupConstants.Completed));
var result = await Task.WhenAll(taskList);
谢谢
为了用尽可能少的语言告诉它,您应该将任务(没有 await
)分配给变量。然后用 WhenAll
等待他们,最后在 Result
属性.
您可以在WhenAll
returns后阅读结果。您可以使用 await
(在完成的任务上再次调用它不会执行任何操作)或从 Result
.
var pendingTask = _lookupService.GetFirstOrDefaultAsync<StatusLookup>(x => x.Code == LookupConstants.Pending);
var cancelTask = _lookupService.GetFirstOrDefaultAsync<StatusLookup>(x => x.Code == LookupConstants.Cancel);
var completedTask = _lookupService.GetFirstOrDefaultAsync<StatusLookup>(x => x.Code == LookupConstants.Completed);
await Task.WhenAll( new [] { pendingTask, cancelTask, completedTask } );
_pending = pendingTask.Result;
_cancel = cancelTask.Result;
_complete = completedTask.Result;
您甚至可以跳过 WhenAll
。这会给你一个稍微不同的行为,但它可能同样有效:
var pendingTask = _lookupService.GetFirstOrDefaultAsync<StatusLookup>(x => x.Code == LookupConstants.Pending);
var cancelTask = _lookupService.GetFirstOrDefaultAsync<StatusLookup>(x => x.Code == LookupConstants.Cancel);
var completedTask = _lookupService.GetFirstOrDefaultAsync<StatusLookup>(x => x.Code == LookupConstants.Completed);
_pending = await pendingTask;
_cancel = await cancelTask;
_complete = await completedTask;