如何理解 async 和await 设计模式 和如何应用到.net 4以下的framework
来源:互联网 发布:qq刷钻软件制作 编辑:程序博客网 时间:2024/06/05 16:25
因为开发了一些silverlight的东西,对于异步编程是深恶痛绝,把逻辑撕裂,非常难受。
今天在stackOverflow网站看到一个很好的解释,摘抄并发挥一下,
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:
Would be translated to something like:
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块,一个是费时工作的前序,一个是费时的工作,还有一个是费时工作的后续,
被翻译成状态机
事实上我们亦可以在.net framework 4以下向上面的状态机类一样来调用异步函数,这样同步调用的逻辑还是保持住了,也更好理解,最大的好处是对于循环的处理简单多了。
今天在stackOverflow网站看到一个很好的解释,摘抄并发挥一下,
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 起始态: this._v1 = 前序工作(); this._await1 = 费时工作(); // 当费时工作异步完成时, 异步工作将改变状态变量值 this.状态变量= 顺利完成; 继续调用自身即 Step函数; //op.SetContinuation(Step); case 顺利完成: this._v2 = this._await1.Result; //费时工作结果 后续工作(this._v1, this._v2); }}
事实上我们亦可以在.net framework 4以下向上面的状态机类一样来调用异步函数,这样同步调用的逻辑还是保持住了,也更好理解,最大的好处是对于循环的处理简单多了。
- 如何理解 async 和await 设计模式 和如何应用到.net 4以下的framework
- Thread,ThreadPool,Task, 到async await 的基本使用方法和理解
- WP8的async和await
- WP8的async和await
- await和async的使用方法
- C#的async和await
- C#的async和await
- C#的async和await
- 快速理解和使用 ES7 await/async
- 快速理解和使用 ES7 await/async
- .NET中使用异步Async和Await的代价
- Async 和 Await 技术
- await 和 Async 初探
- async和await
- Async和Await
- async 和 await
- async和await 用法
- async和await
- 云计算:程序员重回个人英雄时代
- 美国云计算印象
- queue(3)--freebsd中的queue结构使用
- 2012年 上半年 第十周 C++程序设计 (三十三)
- tomcat+mysql数据库连接池的操作
- 如何理解 async 和await 设计模式 和如何应用到.net 4以下的framework
- 《计算机组成原理》第05章在线测试
- Android---自定义带CheckBox的ListView实现
- 分卷压缩和分卷解压
- JavaScript中的闭包
- QTreeView对象如何设置行高(Row Height)?
- NAT的完全分析及其UDP穿透的完全解决方案
- 读书-<<禅修程序员十诫 [译文]>>
- 自定义TRACE函数,用于向Output Window、控制台和文件输出Log