43

在返回任务的异步方法结束时,如果我调用另一个异步方法,我可以调用await它或return它的任务。各自的后果是什么?

    Task FooAsync()
    {
        return BazAsync();  // Option A
    }

    async Task BarAsync()
    {
        await BazAsync(); // Option B
    }
4

2 回答 2

35

如果方法本身被声明为,则无法返回任务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();
}
于 2013-07-26T17:03:57.427 回答
2

查看描述它的链接: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();
}
于 2013-07-26T17:01:40.833 回答