• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

Async await 关键字是否等同于 ContinueWith lambda?

用户头像
it1352
帮助1

问题说明

有人可以确认我是否正确理解了 Async await 关键字吗?(使用 CTP 版本 3)

Could someone please be kind enough to confirm if I have understood the Async await keyword correctly? (Using version 3 of the CTP)

到目前为止,我已经发现在方法调用之前插入 await 关键字基本上做了两件事,A.它创建了一个立即返回,B.它创建了一个继续"在异步方法调用完成时调用.在任何情况下,continuation 都是该方法的代码块的其余部分.

Thus far I have worked out that inserting the await keyword prior to a method call essentially does 2 things, A. It creates an immediate return and B. It creates a "continuation" that is invoked upon the completion of the async method invocation. In any case the continuation is the remainder of the code block for the method.

所以我想知道的是,这两部分代码在技术上是否等效,如果是,这是否基本上意味着 await 关键字与创建 ContinueWith Lambda 相同(即:它基本上是一个编译器的快捷方式)?如果不是,有什么区别?

So what I am wondering is, are these two bits of code technically equivalent, and if so, does this basically mean that the await keyword is identical to creating a ContinueWith Lambda (Ie: it's basically a compiler shortcut for one)? If not, what are the differences?

bool Success =
    await new POP3Connector(
        "mail.server.com", txtUsername.Text, txtPassword.Text).Connect();
// At this point the method will return and following code will
// only be invoked when the operation is complete(?)
MessageBox.Show(Success ? "Logged In" : "Wrong password");

VS

(new POP3Connector(
    "mail.server.com", txtUsername.Text, txtPassword.Text ).Connect())
.ContinueWith((success) =>
    MessageBox.Show(success.Result ? "Logged In" : "Wrong password"));

正确答案

#1

总体思路是正确的 - 方法的其余部分变成了排序的延续.

The general idea is correct - the remainder of the method is made into a continuation of sorts.

快速路径"博客文章 详细介绍了 async/await 编译器转换的工作原理.

The "fast path" blog post has details on how the async/await compiler transformation works.

差异,在我的脑海中:

await 关键字还使用了调度上下文"概念.如果存在,调度上下文是 SynchronizationContext.Current,回退到 TaskScheduler.Current.然后在调度上下文上运行延续.因此,更接近的近似值是将 TaskScheduler.FromCurrentSynchronizationContext 传递到 ContinueWith 中,必要时返回 TaskScheduler.Current.

The await keyword also makes use of a "scheduling context" concept. The scheduling context is SynchronizationContext.Current if it exists, falling back on TaskScheduler.Current. The continuation is then run on the scheduling context. So a closer approximation would be to pass TaskScheduler.FromCurrentSynchronizationContext into ContinueWith, falling back on TaskScheduler.Current if necessary.

实际的async/await 实现是基于模式匹配的;它使用可等待"模式,允许等待除任务之外的其他事情.一些示例是 WinRT 异步 API、一些特殊方法,例如 Yield、Rx observables 和 不会对 GC 造成严重影响的特殊套接字等待项.任务很强大,但它们并不是唯一的可等待对象.

The actual async/await implementation is based on pattern matching; it uses an "awaitable" pattern that allows other things besides tasks to be awaited. Some examples are the WinRT asynchronous APIs, some special methods such as Yield, Rx observables, and special socket awaitables that don't hit the GC as hard. Tasks are powerful, but they're not the only awaitables.

我想到了一个更小的挑剔差异:如果 awaitable 已经完成,那么 async 方法实际上不会在那个时候返回;它同步继续.所以这有点像传递TaskContinuationOptions.ExecuteSynchronously,但没有与堆栈相关的问题.

One more minor nitpicky difference comes to mind: if the awaitable is already completed, then the async method does not actually return at that point; it continues synchronously. So it's kind of like passing TaskContinuationOptions.ExecuteSynchronously, but without the stack-related problems.

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /reply/detail/tanhcgkaja
系列文章
更多 icon
同类精品
更多 icon
继续加载