[英]At the end of an async method, should I return or await?
在返回任务的异步方法结束时,如果我调用另一个异步方法,我可以await
它或return
它的任务。 各自的后果是什么?
Task FooAsync()
{
return BazAsync(); // Option A
}
async Task BarAsync()
{
await BazAsync(); // Option B
}
如果方法本身被声明为async
则您无法返回任务 - 因此这将不起作用,例如:
async Task BarAsync()
{
return BazAsync(); // Invalid!
}
这将需要Task<Task>
的返回类型。
如果您的方法只是做少量工作,然后只调用一个异步方法,那么您的第一个选项就可以了,这意味着所涉及的任务少了一个。 您应该知道,同步方法中抛出的任何异常都将同步传递 - 实际上,这就是我更喜欢处理参数验证的方式。
这也是实现重载的常见模式,例如通过取消令牌。
请注意,如果您需要更改以等待其他内容,则需要将其设为异步方法。 例如:
// Version 1:
Task BarAsync()
{
// No need to gronkle yet...
return BazAsync();
}
// Oops, for version 2 I need to do some more work...
async Task BarAsync()
{
int gronkle = await GronkleAsync();
// Do something with gronkle
// Now we have to await BazAsync as we're now in an async method
await BazAsync();
}
查看描述它的链接: http : //msdn.microsoft.com/en-us/library/vstudio/hh191443.aspx
async Task<int> TaskOfTResult_MethodAsync()
{
int hours;
// . . .
// The body of the method should contain one or more await expressions.
// Return statement specifies an integer result.
return hours;
}
// Calls to TaskOfTResult_MethodAsync from another async method.
private async void CallTaskTButton_Click(object sender, RoutedEventArgs e)
{
Task<int> returnedTaskTResult = TaskOfTResult_MethodAsync();
int intResult = await returnedTaskTResult;
// or, in a single statement
//int intResult = await TaskOfTResult_MethodAsync();
}
// Signature specifies Task
async Task Task_MethodAsync()
{
// . . .
// The body of the method should contain one or more await expressions.
// The method has no return statement.
}
// Calls to Task_MethodAsync from another async method.
private async void CallTaskButton_Click(object sender, RoutedEventArgs e)
{
Task returnedTask = Task_MethodAsync();
await returnedTask;
// or, in a single statement
//await Task_MethodAsync();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.