欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

如何理解async和await设计模式和如何应用到.net 4以下的framework

程序员文章站 2023-12-12 21:45:22
因为开发了一些silverlight的东西,对于异步是深恶痛绝,把逻辑撕裂,非常难受。 今天在*网站看到一个很好的解释,摘抄并发挥一下, it works similarly...

因为开发了一些silverlight的东西,对于异步是深恶痛绝,把逻辑撕裂,非常难受。

今天在*网站看到一个很好的解释,摘抄并发挥一下,

it works similarly to the yield return keyword in c# 2.0.

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:
      foo();
      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);
  }
}

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.

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 :-)).

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.

answered by tomas petricek

大致是说 async 和await模拟了一个状态机(上面的_state变量维持状态),async把一个顺序执行的工作分割成3块,一个是费时工作的前序,一个是费时的工作,还有一个是费时工作的后续,

async task demo() {
  var v1 = 前序工作();
  var v2 = await 费时工作();
  后续工作(v1, v2);
}

被翻译成状态机

class 状态机{
    int _v1, _v2;
    int 状态变量 =起始态;  
    task<int> _await1; 
    public void step() {   
        switch(this.状态变量) {  
            case 起始态:      
                foo();
                this._v1 = 前序工作();
                this._await1 = 费时工作();     
                // 当费时工作异步完成时, 异步工作将改变状态变量值
                this.状态变量= 顺利完成;     
                继续调用自身即 step函数;    //op.setcontinuation(step);   
            case 顺利完成:     
                this._v2 = this._await1.result; //费时工作结果    
                后续工作(this._v1, this._v2); 
    }
}

事实上我们亦可以在.net framework 4以下想上面的状态机类一样来调用异步函数,这样同步调用的逻辑还是保持住了,也更好理解,对于循环的处理简单多了。



摘自 cjq1234的专栏

上一篇:

下一篇: