请问有没有好心人能够确认我对 Async Await 关键字的理解是否正确?(使用 CTP 版本 3)
到目前为止,我已经明白在方法调用之前插入 await 关键字实际上做了两件事情:A. 它创建了一个立即返回和 B. 它创建了一个“续体”,该续体在异步方法调用完成后被调用。无论如何,该续体都是该方法代码块的剩余部分。
所以我的问题是,这两段代码是否在技术上等效,如果是,这是否意味着 await 关键字与创建一个 ContinueWith Lambda 相同(即:它基本上是一个编译器的快捷方式)?如果不是,它们有什么区别?
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");
Visual Studio
(new POP3Connector(
"mail.server.com", txtUsername.Text, txtPassword.Text ).Connect())
.ContinueWith((success) =>
MessageBox.Show(success.Result ? "Logged In" : "Wrong password"));
await
仍然像以前一样捕获SynchronizationContext.Current
。但是在ASP.NET Core上,SynchronizationContext.Current
为null
。 - Stephen Cleary