C#异步 - 它是如何工作的? [英] C# Async - How does it work?

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

问题描述

微软异步CTP 今日(2010年10月28日)公布的的Visual Studio的介绍异步等待关键字,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.

首先,我认为,编译器会将关键字,创建一个线程,但根据<一个href="http://www.microsoft.com/downloads/en/confirmation.aspx?FamilyID=d7ccfefa-123a-40e5-8ed5-8d2edd68acf4&displaylang=en">white纸和安德斯Hejlsberg为的 PDC presentation (在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。

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

这是异步方法实际上不是一个普通的顺序法。它被编译成与一些状态的状态机(一个对象)(局部变量被接通到对象的字段)。的code两种用途之间的每一块等待是国家机器的一个台阶。

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.

这意味着该方法开始时,它只是运行的第一步,然后状态机返回和日程安排一些工作要做 - 当工作完成后,它将运行状态机的下一个步骤。例如,这code:

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 方法来指定的操作完成时,它应该叫步骤再次方法(该方法知道它应该使用 _state 字段运行原来的code中的第二位)。你可以很容易想象, SetContinuation 将类似于 btn.Click + =步,这将在一个完全运行单个线程。

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#异步工作流(实际上,它在本质上是一样的东西,除了一些技术细节),和写作使用异步反应单线程GUI应用程序是一个相当有趣的领域 - 至少我这么认为 - 例如参见<一个href="http://dotnetslackers.com/articles/net/Programming-user-interfaces-using-f-sharp-workflows.aspx">this文章(也许我应该写信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 :-)).

翻译类似于迭代器(和收益率回报),并在事实上,这是可以使用迭代器来实现在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天全站免登陆