异步在 C# 中是如何工作的? [英] How does async works in C#?

查看:16
本文介绍了异步在 C# 中是如何工作的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

微软今天(10 月 28 日)宣布了 Visual Studio Async CTP, 2010) 将 asyncawait 关键字引入 C#/VB 以进行异步方法执行.

Microsoft announced the Visual Studio Async CTP today (October 28, 2010) that introduces the async and await keywords into C#/VB for asynchronous method execution.

首先我认为编译器将关键字翻译成线程的创建但是根据白皮书 和 Anders Hejlsberg 的 PDC 演示(31:00)异步操作完全发生在主线程上.

First I thought that the compiler translates the keywords into the creation of a thread but according to the white paper and Anders Hejlsberg's PDC presentation (at 31:00) the asynchronous operation happens completely on the main thread.

如何在同一线程上并行执行操作?它在技术上是如何可能的,在 IL 中实际翻译的功能是什么?

How can I have an operation executed in parallel on the same thread? How is it technically possible and to what is the feature actually translated in IL?

推荐答案

它的工作原理类似于 C# 2.0 中的 yield return 关键字.

It works similarly to the yield return keyword in C# 2.0.

异步方法实际上不是普通的顺序方法.它被编译成一个带有一些状态的状态机(一个对象)(局部变量变成了对象的字段).await 两次使用之间的每个代码块都是状态机的一个步骤".

An asynchronous method is not actually an ordinary sequential method. It is compiled into a state machine (an object) with some state (local variables are turned into fields of the object). Each block of code between two uses of await is one "step" of the state machine.

这意味着当方法启动时,它只是运行第一步,然后状态机返回并安排一些要完成的工作——当工作完成时,它会运行状态机的下一步.例如这段代码:

This means that when the method starts, it just runs the first step and then the state machine returns and schedules some work to be done - when the work is done, it will run the next step of the state machine. For example this code:

async Task Demo() { 
  var v1 = foo();
  var v2 = await bar();
  more(v1, v2);
}

会被翻译成类似的东西:

Would be translated to something like:

class _Demo {
  int _v1, _v2;
  int _state = 0; 
  Task<int> _await1;
  public void Step() {
    switch(this._state) {
    case 0: 
      this._v1 = foo();
      this._await1 = bar();
      // When the async operation completes, it will call this method
      this._state = 1;
      op.SetContinuation(Step);
    case 1:
      this._v2 = this._await1.Result; // Get the result of the operation
      more(this._v1, this._v2);
  }
}

重要的部分是它只是使用了SetContinuation 方法来指定当操作完成时,它应该再次调用Step 方法(并且该方法知道它应该使用 _state 字段运行原始代码的第二位).您可以很容易地想象SetContinuation 将类似于btn.Click += Step,它会完全在单个线程上运行.

The important part is that it just uses the SetContinuation method to specify that when the operation completes, it should call the Step method again (and the method knows that it should run the second bit of the original code using the _state field). You can easily imagine that the SetContinuation would be something like btn.Click += Step, which would run completely on a single thread.

C# 中的异步编程模型非常接近 F# 异步工作流(实际上,除了一些技术细节之外,本质上是一回事),并且使用 async 是一个非常有趣的领域 - 至少我认为是 - 参见例如 这篇文章(也许我现在应该写一个 C# 版本 :-)).

The asynchronous programming model in C# is very close to F# asynchronous workflows (in fact, it is essentially the same thing, aside from some technical details), and writing reactive single-threaded GUI applications using async is quite an interesting area - at least I think so - see for example this article (maybe I should write a C# version now :-)).

翻译类似于迭代器(和yield return),事实上,早先可以使用迭代器在C#中实现异步编程.我不久前写了一篇关于此的文章 - 我认为它仍然可以给你一些深入了解翻译的工作原理.

The translation is similar to iterators (and yield return) and in fact, it was possible to use iterators to implement asynchronous programming in C# earlier. I wrote an article about that a while ago - and I think it can still give you some insight on how the translation works.

这篇关于异步在 C# 中是如何工作的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆