异步方法中的最后一个异步调用需要等待吗? [英] does last async call in async method require await?

查看:47
本文介绍了异步方法中的最后一个异步调用需要等待吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当异步方法调用是我的异步方法中的最后一个调用时,我无法理解是否需要await语句.例如

I can't understand if await statement is required when async method call is the last call in my async method. E.g.

public async Task CallAsync(byte[] data)
{
    await Call1Async(data);
    Call2Async(data);
}

public async Task Call1Async(byte[] data)
{
   ...
}

public async Task Call2Async(byte[] data)
{
   ...
}

上面的代码可以编译,但带有警告请考虑将等待方式应用于此方法".但是我认为,等待该方法中的最后一次调用会浪费资源.

The above would compile but with a warning "consider applying await to this method". But I think it would be waste of resources to apply await for the last call in the method.

同时,如果我使用返回Call2Async(data); 而不只是 Call2Async(data); ,该警告就会消失.这表明在这种情况下实际上不需要 await .

At the same time, if I use return Call2Async(data); instead of just Call2Async(data);, the warning would go away. This indicates await is not actually required in such situation.

但这仅适用于具有返回值的方法(即 Task< T> 而不只是 Task ).对于没有返回值的方法,我需要此方法.有什么方法可以实现价值回报方法?

But this only works for methods which have a return value (i.e. Task<T> rather than just Task). I need this for methods without return values. Is there any way to do the same for value-returning methods?

即我需要以某种方式将 Call2Async 返回的 Task 对象返回给 CallAsync 的调用者,而我不能使用 return CallAsync 中的声明显式,因为我的方法没有返回值.当然,我可以将它们全部调整为返回值(例如true),但这不是一个很好的解决方案.

I.e. I need to somehow return Task object returned by Call2Async to the caller of CallAsync and I can't use return statement in CallAsync explicitly as my methods don't have return values. Of course, I can adjust them all to return values (e.g. true) but it would not be an elegant solution.

P.S.如果我的方法中只有异步调用,则只需在方法签名中不添加"async"即可,并且它可以工作(不需要"return"语句).但是,如果该方法包含多个异步调用,则我需要具有异步"修饰符,以便能够等待第一个调用"await Call1Async(data)".并且添加'async'修饰符使编译器需要'return'才能将Task对象返回给调用者(这仅适用于具有返回值的方法).这就是我要克服的问题.

P.S. If I have the only async call in my method, I just don't add 'async' to the method signature, and it works ('return' statement not required). But if the method contains more than one async call, I need to have 'async' modifier to be able to await for the first call 'await Call1Async(data)'. And adding 'async' modifier makes the compiler require 'return' to return Task object to the caller (and this works only for methods with return values). That's what I'm trying to overcome.

推荐答案

通过以下方法:

public async Task CallAsync(byte[] data)
{
    await Call1Async(data);
    Call2Async(data);
}

该方法的控件在开始 Call2Async 后返回.也就是说,如果您要等待CallAsync(data),它将在 Call2Async 结束之前完成.这可能不是您想要的.仅当两个调用都完成时,此方法才会完成:

Control from the method returns after beginning Call2Async. That is, if you were to await CallAsync(data), it would finish before Call2Async ends. This is probably not what you intended. This method will only finish when both calls are complete:

public async Task CallAsync(byte[] data)
{
    await Call1Async(data);
    await Call2Async(data);
}

您可以在这些示例输出中看到差异(我实现了两个异步调用,以简单地打印开始,延迟一秒钟然后打印结束).

You can see the difference in these example outputs (I implemented the two async calls to simply print a start, delay for a second, and then print an end).

void Main()
{
    Console.WriteLine("Main start");
    CallAsync(null).Wait();
    Console.WriteLine("Main end");
}

不打印第二个 await 的方法:

Main start
Call1Async start
Call1Async end
Call2Async start
Main end
Call2Async end

第二次 await :

Main start
Call1Async start
Call1Async end
Call2Async start
Call2Async end
Main end

此实现也有一个问题:

public Task CallAsync(byte[] data)
{
    Call1Async(data);
    return Call2Async(data);
}

它同时启动两个异步调用,但是无论 Call1Async 是否完成,它都在 Call2Async 完成时返回控制.同样,这可能不是您想要的.

It kicks off both async calls at the same time, but then returns control when Call2Async completes, whether Call1Async is complete or not. Again, this is probably not what you intended.

这篇关于异步方法中的最后一个异步调用需要等待吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆