本文介绍了是异步伺机关键字相当于一个ContinueWith拉姆达?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能有人请仁慈地确认,如果我理解了异步关键字正确等待? (使用CTP版本3)

到目前为止,我已经摸索出插入伺机关键字之前的方法调用本质上是做两件事情,A.它创建一个即时回报和B.它创建一个异步方法完成后调用的一个延续调用。在任何情况下,延续是该方法的code座的剩余部分。

那么,我想知道的,是code的这两位技术上等效的,如果是这样,那么这基本上意味着的await关键字是与创建ContinueWith LAMBDA(即:为它基本上是一个编译器快捷方式一)?如果不是,有什么区别?

  BOOL成功=
    等待新POP3Connector(
        mail.server.com,txtUsername.Text,txtPassword.Text).Connect();
//此时该方法将返回并按照code会
当操作完成//仅被调用(?)
MessageBox.Show(?成功登陆:密码错误);

VS

 (新POP3Connector(
    mail.server.com,txtUsername.Text,txtPassword.Text).Connect())
.ContinueWith((成功)=>
    的MessageBox.show(success.Result登录:密码错误));


解决方案

总的想法是正确的 - 该方法的其余部分被制作成各种各样的延续

借助对细节如何使用异步 / 等待编译器改造工作。

的差异,把我的头顶部:

等待关键字也使用了调度语境的概念。调度上下文是 SynchronizationContext.Current 如果存在的话,回落在 TaskScheduler.Current 。延续随后的调度上下文中运行。所以更接近于将通过 TaskScheduler.FromCurrentSynchronizationContext ContinueWith ,回落在的TaskScheduler .Current ,如果必要的。

实际异步 / 等待的实现是基于模式匹配;它采用了awaitable的格局,让其他的事情,除了要等待任务。一些例子是WinRT的异步API的,一些特殊的方法,如收益,接收观测和的。任务是强大的,但他们不是唯一的awaitables。

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

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

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.

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: its 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"));
解决方案

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

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

Differences, off the top of my head:

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.

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.

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.

这篇关于是异步伺机关键字相当于一个ContinueWith拉姆达?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 08:16