0
.NET平台编程语言  异步特性演变   赵劼 - 2012.2
关于我• 赵劼 / 老赵 / Jeffrey Zhao / 赵姐夫• 日写代码三百行,不辞长作程序员• 博客:http://blog.zhaojie.me/• 微博:@老赵• F#, JavaScript, Scala, C#, Python,...
内容• C# 1.0• C# 2.0• F#• C# 5
C# 1.0
两种简单模型• Begin / End 模型• 事件模型• 两种模型都基于回调函数 •   “异步”所在
Begin / End 模型delegate AsyncCallback(IAsyncResult);interface IAsyncResult {    object AsyncState { get; }    ...}void Begi...
事件模型class XyzCompletedEventArgs : EventArgs {    Exception Error { get; }    TResult Result { get; }}class Target {    eve...
实现异步Transfer方法
示例⼀一C# 1.0原生异步编程
破坏了代码的局部性• 线性代码表达更清晰,更符合习惯• 异步会强迫拆分代码逻辑 •   不能使用 if / using / while / for ...• 难以 •   组合异步操作 •   异常处理 •   取消操作
C# 2.0
“yield” for Iterators  IEnumerable<int> Numbers() {      yield return 0;  	      yield return 1;  	      yield return 2;  }
“yield” for Iterators             IEnumerable<int> Numbers() {MoveNext()                 yield return 0;             	    ...
“yield” for Iterators             IEnumerable<int> Numbers() {MoveNext()                 yield return 0;             	Move...
“yield” for Iterators             IEnumerable<int> Numbers() {MoveNext()                 yield return 0;             	Move...
“yield” for Iterators             IEnumerable<int> Numbers() {MoveNext()                 yield return 0;             	Move...
示例二C# 2.0的yield异步编程
“yield” 之与异步编程• 带来新的异步编程模式• 保持代码局部性 •   优势:支持 if / using / while / for ... •   不完美:不支持 try...catch• 可用来实现 Fibers:轻量的计算单元
F#
F# 编程语言• 微软研究院Don Syme设计• 强类型,静态类型• 函数式编程语言,包含面向对象特性• 面向业界及教育•   开源(Apache 2.0)•   微软提供跨平台支持
并发的挑战• 状态共享 - 不可变性• 代码局部性 - async { ... }• I/O并行 - async { ... }• 扩展至集群 - 使用 async { ... } 的代理
什么是 async { ... }... the principle we go by is, dont expect to seea particular concurrency model put into C#because therer...
异步工作流async { 	    let! res = <async work>    ...	}
异步工作流            React!async { 	    let! res = <async work>    ...	}
异步工作流            React!async { 	    let! res = <async work>    ...	}         HTTP 响应           UI 事件         Timer 回调     ...
async { ... } 工作方式  async {      let! img = AsyncRead "http://..."      printfn "loaded!"      do! AsyncWrite img @"c:..."...
async { ... } 工作方式       async {           let! img = AsyncRead "http://..."           printfn "loaded!"           do! Asy...
示例三F# 异步工作流
C# 5
源代码async Task<XElement> GetRssAsync(string url) {    var client = new WebClient();    var task = client.DownloadStringTask...
编译结果Task<XElement> GetRssAsync(string url) {    var $builder = AsyncMethodBuilder<XElement>.Create();    var $state = 0;  ...
示例四C# 5 异步编程支持
示例五Jscex:JS异步编程
Q &A
谢谢
The Evolution of Async Programming (GZ TechParty C#)
Upcoming SlideShare
Loading in...5
×

The Evolution of Async Programming (GZ TechParty C#)

2,157

Published on

Published in: Technology, News & Politics
0 Comments
4 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
2,157
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
34
Comments
0
Likes
4
Embeds 0
No embeds

No notes for slide

Transcript of "The Evolution of Async Programming (GZ TechParty C#)"

  1. 1. .NET平台编程语言 异步特性演变 赵劼 - 2012.2
  2. 2. 关于我• 赵劼 / 老赵 / Jeffrey Zhao / 赵姐夫• 日写代码三百行,不辞长作程序员• 博客:http://blog.zhaojie.me/• 微博:@老赵• F#, JavaScript, Scala, C#, Python, .NET, Mono...• 痛恨Java语言
  3. 3. 内容• C# 1.0• C# 2.0• F#• C# 5
  4. 4. C# 1.0
  5. 5. 两种简单模型• Begin / End 模型• 事件模型• 两种模型都基于回调函数 • “异步”所在
  6. 6. Begin / End 模型delegate AsyncCallback(IAsyncResult);interface IAsyncResult { object AsyncState { get; } ...}void BeginXyz(arg1, arg2, ..., AsyncCallback, state);TResult EndXyz(IAsyncResult);
  7. 7. 事件模型class XyzCompletedEventArgs : EventArgs { Exception Error { get; } TResult Result { get; }}class Target { event EventHandler<XyzCompletedArgs> XyzCompleted; void XyzAsync(arg1, arg2, ...);}
  8. 8. 实现异步Transfer方法
  9. 9. 示例⼀一C# 1.0原生异步编程
  10. 10. 破坏了代码的局部性• 线性代码表达更清晰,更符合习惯• 异步会强迫拆分代码逻辑 • 不能使用 if / using / while / for ...• 难以 • 组合异步操作 • 异常处理 • 取消操作
  11. 11. C# 2.0
  12. 12. “yield” for Iterators IEnumerable<int> Numbers() { yield return 0; yield return 1; yield return 2; }
  13. 13. “yield” for Iterators IEnumerable<int> Numbers() {MoveNext() yield return 0; yield return 1; yield return 2; }
  14. 14. “yield” for Iterators IEnumerable<int> Numbers() {MoveNext() yield return 0; MoveNext() yield return 1; yield return 2; }
  15. 15. “yield” for Iterators IEnumerable<int> Numbers() {MoveNext() yield return 0; MoveNext() yield return 1; MoveNext() yield return 2; }
  16. 16. “yield” for Iterators IEnumerable<int> Numbers() {MoveNext() yield return 0; MoveNext() yield return 1; MoveNext() yield return 2;MoveNext() }
  17. 17. 示例二C# 2.0的yield异步编程
  18. 18. “yield” 之与异步编程• 带来新的异步编程模式• 保持代码局部性 • 优势:支持 if / using / while / for ... • 不完美:不支持 try...catch• 可用来实现 Fibers:轻量的计算单元
  19. 19. F#
  20. 20. F# 编程语言• 微软研究院Don Syme设计• 强类型,静态类型• 函数式编程语言,包含面向对象特性• 面向业界及教育• 开源(Apache 2.0)• 微软提供跨平台支持
  21. 21. 并发的挑战• 状态共享 - 不可变性• 代码局部性 - async { ... }• I/O并行 - async { ... }• 扩展至集群 - 使用 async { ... } 的代理
  22. 22. 什么是 async { ... }... the principle we go by is, dont expect to seea particular concurrency model put into C#because therere many different concurrencymodel ... its more about finding things arecommon to to all kinds of concurrency ... - Anders Hejlsberg
  23. 23. 异步工作流async {   let! res = <async work> ... }
  24. 24. 异步工作流 React!async {   let! res = <async work> ... }
  25. 25. 异步工作流 React!async {   let! res = <async work> ... } HTTP 响应 UI 事件 Timer 回调 查询结果 Web Servcie 答复 I/O 完成 代理消息
  26. 26. async { ... } 工作方式 async { let! img = AsyncRead "http://..." printfn "loaded!" do! AsyncWrite img @"c:..." printfn "saved!" }
  27. 27. async { ... } 工作方式 async { let! img = AsyncRead "http://..." printfn "loaded!" do! AsyncWrite img @"c:..." printfn "saved!" } =async.Delay(fun -> async.Bind(AsyncRead "http://...", (fun img -> printfn "loaded!" async.Bind(AsyncWrite img @"c:...", (fun () -> printfn "saved!" async.Return())))))
  28. 28. 示例三F# 异步工作流
  29. 29. C# 5
  30. 30. 源代码async Task<XElement> GetRssAsync(string url) { var client = new WebClient(); var task = client.DownloadStringTaskAsync(url); var text = await task; var xml = XElement.Parse(text); return xml;}
  31. 31. 编译结果Task<XElement> GetRssAsync(string url) { var $builder = AsyncMethodBuilder<XElement>.Create(); var $state = 0; TaskAwaiter<string> $a1; Action $resume = delegate { try { if ($state == 1) goto L1; var client = new WebClient(); var task = client.DownloadStringTaskAsync(url); $state = 1; $a1 = task.GetAwaiter(); if ($a1.BeginAwait($resume)) return; L1: var text = $a1.EndAwait(); var xml = XElement.Parse(text); $builder.SetResult(xml); } catch (Exception $ex) { $builder.SetException($ex); } }; $resume(); return $builder.Task;}
  32. 32. 示例四C# 5 异步编程支持
  33. 33. 示例五Jscex:JS异步编程
  34. 34. Q &A
  35. 35. 谢谢
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×