更新时间:2023-02-24 21:14:28
其中一个主要的指导方针以书面形式异步
code是避异步无效
- 即,使用异步任务
而不是的异步无效
除非你'重新实施一个异步
事件处理程序。
One of the primary guidelines in writing async
code is "avoid async void
" - that is, use async Task
instead of async void
unless you're implementing an async
event handler.
异步无效
方法使用的 OperationStarted
的SynchronizationContext
和 OperationCompleted
;看到我的MSDN文章这是所有关于SynchronizationContext的了解更多详情。
async void
methods use SynchronizationContext
's OperationStarted
and OperationCompleted
; see my MSDN article It's All about the SynchronizationContext for more details.
ASP.NET检测调用 OperationStarted
和(正确地)拒绝它,因为它是非法的把一个异步
事件处理程序存在。当你纠正code使用异步任务
,然后ASP.NET不再看到一个异步
事件处理程序
ASP.NET detects the call to OperationStarted
and (correctly) rejects it because it's illegal to put an async
event handler there. When you correct the code to use async Task
, then ASP.NET no longer sees an async
event handler.
您可能会发现我的介绍到异步
/ 等待
帖子有帮助的。
You may find my intro to async
/ await
post helpful.