且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

异步关键字和的TaskScheduler的选择

更新时间: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.