Yu Zhaohui http://yuyijq.cnblogs.com .NET 平台上的异步编程
<ul><li>余昭辉,网名横刀天笑 </li></ul><ul><li>http://yuyijq.cnblogs.com </li></ul><ul><li>Programming language fans </li></ul><ul><...
90% 以上的应用都是在以错误的方式使用线程   - Jeffrey Richter
 
 
Write async program is hard,  and write stable program is even harder.
<ul><li>private void btnDownload_Click(object sender, EventArgs e) </li></ul><ul><li>{ </li></ul><ul><li>try{ </li></ul><u...
 
var request = HttpWebRequest.Create(“http://yuyijq.cnblogs.com”); request. BeginGetResponse (DownloadCompeleted,request); ...
传统异步的异步编程 <ul><li>For, while  等基本构造无法使用 </li></ul><ul><li>Try catch 无法使用,异常处理非常困难 </li></ul><ul><li>Using 无法使用,代码变糟 </li><...
其实我们要寻找的是一种获得 continue 的方式:   Continution Passing Style
Print(Add(5,6)) void Print(int result) { Console.WriteLine(result); } int Add(int l, int r) { return l + r; } Add(5, 6, (r...
神奇的 yield Private IEnumerator<int> Caclute() { int i = 5 + 6; yield return 1; Console.WriteLine(i); i = 4 * 5; yield retur...
如是我们可以这样编写异步代码: Private Ienumerator<int> Download() { var req = HttpWebRequest.Create(“http://yuyijq.cnblogs.com”); var ar...
AsyncRunner runner = new AsyncRunner(); Runner.Run(Download(runner)); Public void Run(Ienumerator<int> async) { this.async...
Private Ienumerator<int> Download(AsyncRunner runner) { var req = HttpWebRequest.Create(“http://yuyijq.cnblogs.com”); var ...
第三方类库支持 <ul><li>Jeffrey Ritcher 的 AsyncEnumerator </li></ul><ul><li>CCR 的 Iterator </li></ul><ul><li>Rx(reactive programmi...
F# 简介 <ul><li>由微软剑桥研究院的  Don Syme 开发 </li></ul><ul><li>静态的,强类型的,基于 .NET 平台的函数式编程语言 </li></ul><ul><li>历史基于和 .NET 一样长 </li><...
F# ASYNC WORKFLOW def download url = async{ let req = WebRequest.Create(url) let!  resp = req.AsyncGetResponse() use strea...
F# WORKFLOW type Async<‘T> type AsyncBuilder with member Return : ‘T -> Async<‘T> member Delay : (unit -> Async<‘T>) -> As...
.NET 5.0 TAP Public async String Download(String url) { var req = WebRequest.Create(url); var resp = await req.GetResponse...
Asynchrony doesn't mean &quot;background thread”   Lucian Wischik
Thank you
Upcoming SlideShare
Loading in …5
×

Async programming on NET

1,267 views
1,123 views

Published on

.NET平台上的异步编程风格的变化,从传统的异步编程,到利用第三方类库以及新出现的F#异步工作流,还有未来.NET 5.0中的TAP

Published in: Self Improvement, Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
1,267
On SlideShare
0
From Embeds
0
Number of Embeds
8
Actions
Shares
0
Downloads
11
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Async programming on NET

  1. 1. Yu Zhaohui http://yuyijq.cnblogs.com .NET 平台上的异步编程
  2. 2. <ul><li>余昭辉,网名横刀天笑 </li></ul><ul><li>http://yuyijq.cnblogs.com </li></ul><ul><li>Programming language fans </li></ul><ul><li>Design Pattern & Architecture </li></ul><ul><li>.NET CLR </li></ul><ul><li>C# MVP </li></ul>
  3. 3. 90% 以上的应用都是在以错误的方式使用线程 - Jeffrey Richter
  4. 6. Write async program is hard, and write stable program is even harder.
  5. 7. <ul><li>private void btnDownload_Click(object sender, EventArgs e) </li></ul><ul><li>{ </li></ul><ul><li>try{ </li></ul><ul><ul><ul><li>var request = HttpWebRequest.Create(“http://yuyijq.cnblogs.com”); </li></ul></ul></ul><ul><ul><ul><li>var response = request.GetResponse(); </li></ul></ul></ul><ul><ul><ul><li>using (var stream = response.GetResponseStream()) </li></ul></ul></ul><ul><ul><ul><li>{ </li></ul></ul></ul><ul><ul><ul><li>return stream.ReadToEnd(); </li></ul></ul></ul><ul><ul><ul><li>} </li></ul></ul></ul><ul><ul><ul><li>}catch{//…} </li></ul></ul></ul><ul><li>} </li></ul>
  6. 9. var request = HttpWebRequest.Create(“http://yuyijq.cnblogs.com”); request. BeginGetResponse (DownloadCompeleted,request); public void DownloadCompeleted(IAsyncResult ar) { var request = (HttpWebRequest)ar.AsyncState; var response = request. EndGetResponse (); var stream = response.GetResponseStream(); ReadHelper(stream); } public void ReadHelper(Stream stream) { byte[] buffer = new byte[1024]; stream.BeginRead(buffer,0,1024,(ar) =>{ var actualRead = stream.EndRead(); if(actualRead == 0){ stream.Close(); }else{ //proccess data ReadHelper(stream); } },null); }
  7. 10. 传统异步的异步编程 <ul><li>For, while 等基本构造无法使用 </li></ul><ul><li>Try catch 无法使用,异常处理非常困难 </li></ul><ul><li>Using 无法使用,代码变糟 </li></ul><ul><li>所有异步方法分为二段式:发起请求和请求结束处理。代码进一步腐化 </li></ul><ul><li>这样造成的结果是: </li></ul><ul><li>放弃效率更高的异步方式,采用效率低的同步方式 </li></ul>
  8. 11. 其实我们要寻找的是一种获得 continue 的方式: Continution Passing Style
  9. 12. Print(Add(5,6)) void Print(int result) { Console.WriteLine(result); } int Add(int l, int r) { return l + r; } Add(5, 6, (ret) => Print(ret)); void Add(int l, int r, Action<int> continue) { continue(l + r); }
  10. 13. 神奇的 yield Private IEnumerator<int> Caclute() { int i = 5 + 6; yield return 1; Console.WriteLine(i); i = 4 * 5; yield return 1; Console.WriteLine(i); //… } Private Ienumerator<int> Caclute() { return new StateMachine(); } Public class StateMachine { public bool MoveNext() { switch(state) { case 0: //… } } } While(Caclute().MoveNext()){ //… }
  11. 14. 如是我们可以这样编写异步代码: Private Ienumerator<int> Download() { var req = HttpWebRequest.Create(“http://yuyijq.cnblogs.com”); var ar1 = req.BeginGetResponse(__?__,null); yield return 1; var resp = req.EndGetResponse(ar1); using(var stream = resp.GetResponseStream()) { byte[] buffer = new byte[1024]; do{ var ar2 =stream.BeginRead(buffer,0,1024,__?__,null); yield return 1; var actualRead = stream.EndRead(ar2); }while(actualRead != 0) } } self.MoveNext self.MoveNext
  12. 15. AsyncRunner runner = new AsyncRunner(); Runner.Run(Download(runner)); Public void Run(Ienumerator<int> async) { this.async = async; async.MoveNext(); } Public AsyncCallback Continue() { return (ar) => this.async.MoveNext(); }
  13. 16. Private Ienumerator<int> Download(AsyncRunner runner) { var req = HttpWebRequest.Create(“http://yuyijq.cnblogs.com”); var ar1 = req.BeginGetResponse( runner.Continue ,null); yield return 1; var resp = req.EndGetResponse(ar1); using(var stream = resp.GetResponseStream()) { byte[] buffer = new byte[1024]; do{ var ar2 =stream.BeginRead(buffer,0,1024, runner.Continue ,null); yield return 1; var actualRead = stream.EndRead(ar2); }while(actualRead != 0) } }
  14. 17. 第三方类库支持 <ul><li>Jeffrey Ritcher 的 AsyncEnumerator </li></ul><ul><li>CCR 的 Iterator </li></ul><ul><li>Rx(reactive programming) </li></ul>
  15. 18. F# 简介 <ul><li>由微软剑桥研究院的 Don Syme 开发 </li></ul><ul><li>静态的,强类型的,基于 .NET 平台的函数式编程语言 </li></ul><ul><li>历史基于和 .NET 一样长 </li></ul><ul><li>起源于 Ocame </li></ul>
  16. 19. F# ASYNC WORKFLOW def download url = async{ let req = WebRequest.Create(url) let! resp = req.AsyncGetResponse() use stream = resp.GetResponseStream() use reader = new StreamReader(stream) let! content = reader.AsyncReadToEnd() return content } builder.Delay( var req = WebRequest.Create(url); builder.Bind(req.AsyncGetReponse(),(resp) =>{ builder.Using(resp.GetResponseStream(),(stream) =>{ builder.Using(new StreamReader(stream),(reader) =>{ builder.Bind(reader.AsyncReadToEnd(),(content) =>{ builder.Return(content); }) }) }) }) );
  17. 20. F# WORKFLOW type Async<‘T> type AsyncBuilder with member Return : ‘T -> Async<‘T> member Delay : (unit -> Async<‘T>) -> Async<‘T> member Using : ‘T * (‘T -> Async<‘U>) -> Async<‘U> when ‘T :> System.Idisposable member Bind : Async<‘T> * (‘T -> Async<‘U>) -> Async<‘U> let async = new AsyncBuilder async{ let!... }
  18. 21. .NET 5.0 TAP Public async String Download(String url) { var req = WebRequest.Create(url); var resp = await req.GetResponseAsync(); using(var stream = resp.GetResponseStream()) { } }
  19. 22. Asynchrony doesn't mean &quot;background thread” Lucian Wischik
  20. 23. Thank you

×