c# - 在没有等待的情况下使用异步?

标签 c# task-parallel-library async-await c#-5.0

考虑 Using async without await .

think that maybe you misunderstand what async does. The warning is exactly right: if you mark your method async but don't use await anywhere, then your method won't be asynchronous. If you call it, all the code inside the method will execute synchronously.

我想写一个应该异步运行但不需要使用 await 的方法。例如,当使用线程时

public async Task PushCallAsync(CallNotificationInfo callNotificationInfo)
{
    Logger.LogInfo("Pushing new call {0} with {1} id".Fill(callNotificationInfo.CallerId,
}

我想调用 PushCallAsync 并运行异步,不想使用 await。

我可以在 C# 中不使用 await 而使用 async 吗?

最佳答案

你还在误解asyncasync 关键字意味着“在另一个线程上运行”。

要将一些代码推送到另一个线程,您需要明确地执行此操作,例如,Task.Run:

await Task.Run(() => Logger.LogInfo("Pushing new call {0} with {1} id".Fill(callNotificationInfo.CallerId));

我有一个 async/await intro post您可能会觉得有帮助。

关于c# - 在没有等待的情况下使用异步?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17805887/

相关文章:

c# - 什么是创建可等待函数的正确方法

c# - 使用 async/await/TPL 的这两个函数有什么区别?

c# - 等待同步的异步方法是否可以接受?

c# - 异步 TryParse(...) 模式

c# - 调试程序集加载问题

c# - 在 Razor View 中显示枚举描述

c# - 下游 block 中的 TPL 数据流和异常处理

Javascript:异步函数后退出脚本

c# - 手榴弹在错误的位置生成

c# - ITextSharp 从 5.3.3.0 升级到 5.5.0.0 合并文件问题 - 文档关闭时已关闭