Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/308.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 等待结果还是直接返回任务?_C#_Async Await - Fatal编程技术网

C# 等待结果还是直接返回任务?

C# 等待结果还是直接返回任务?,c#,async-await,C#,Async Await,直接返回任务和先等待结果再返回结果之间有什么区别吗 public Task<Result> GetResultAsync() { return ComposeResultAsync(); // returning task directly } private Task<Result> ComposeResultAsync() { // async code } 对 public async Task<Result> GetResultAs

直接返回任务和先等待结果再返回结果之间有什么区别吗

public Task<Result> GetResultAsync()
{
    return ComposeResultAsync(); // returning task directly
}

private Task<Result> ComposeResultAsync()
{
    // async code
}

public async Task<Result> GetResultAsync()
{
    return await ComposeResultAsync(); // awaiting result first
}

private Task<Result> ComposeResultAsync()
{
    // async code
}
是否有任何理由使用第一个示例,除非我需要等待另一个任务

public async Task<Result> GetResultAsync()
{
    await DoSomethingElseAsync();

    return await ComposeResultAsync();
}

我仍然不确定。

如果一段时间后您需要一个答案,并且您想并行运行两个任务,该怎么办?最好不要等待,而是让他并行工作,在你们真正需要答案的时候等待