考虑 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 吗?
最佳答案
你还在误解async
。 async
关键字不意味着“在另一个线程上运行”。
要将一些代码推送到另一个线程,您需要明确地执行此操作,例如,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/