Presentation is loading. Please wait.

Presentation is loading. Please wait.

Asynchronous Programming with C# v.Next

Similar presentations


Presentation on theme: "Asynchronous Programming with C# v.Next"— Presentation transcript:

1 Asynchronous Programming with C# v.Next
Pavel Yosifovich Hi-Tech College © 2007 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.

2 Agenda C# Evolution Trends Synchronous vs. Asynchronous
Asynchrony the “old way” The New Way Odds and Ends Summary

3 C# Evolution C# 4.0 Dynamic Typing C# 3.0 Language Integrated Query
Generics C# 1.0 Managed Code

4 Trends Declarative Concurrent Dynamic

5 Trends Increasingly connected applications Asynchronous programming
More latency More UI responsiveness problems More scalability issues Asynchronous programming Becoming the norm in responsive, scalable apps Async-only APIs, e.g. JavaScript and Silverlight

6 C# Evolution C# v.Next Asynchronous Programming C# 4.0 Dynamic Typing
Language Integrated Query C# 2.0 Generics C# 1.0 Managed Code

7 demo Responsive UI 5/13/2018 8:08 AM
© 2007 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.

8 Asynchrony in a Nutshell
Synchronous  Wait for result before returning string DownloadString(...); Asynchronous  Return now, call back with result void DownloadStringAsync(..., Action<string> callback); Asynchronous work != Threads

9 Synchronous vs. Asynchronous
var data = DownloadData(...); ProcessData(data); STOP ProcessData DownloadData Thread DownloadDataAsync(... , data => { ProcessData(data); }); Thread DownloadDataAsync ProcessData

10 Synchronous vs. Asynchronous
var data = DownloadData(...); ProcessData(data); STOP ProcessData DownloadData STOP Thread DownloadDataAsync(... , data => { ProcessData(data); }); Thread DownloadDataAsync ProcessData

11 Asynchronous Methods Goal: Just like synchronous programming
Framework: Use Task<T> for all asynchrony Add Task<T> support to existing .NET and Silverlight APIs Developers can do the same Languages: Asynchronous Methods “async” modifier marks method or lambda as asynchronous “await” operator yields control until awaited task completes

12 Task<T> Represents “ongoing operation” Composable callback model
Could be async I/O, background worker, anything... Single object for status, result and exceptions Composable callback model var task2 = task1.ContinueWith(t => … t.Result …); The “await” operator rewrites to continuations Combinators WhenAll, WhenAny, Delay, etc.

13 Asynchronous Control Flow
async void DoWorkAsync() { var t1 = ProcessFeedAsync(" var t2 = ProcessFeedAsync(" await Task.WhenAll(t1, t2); DisplayMessage("Done"); } async Task ProcessFeedAsync(string url) { var text = await DownloadFeedAsync(url); var doc = ParseFeedIntoDoc(text); await SaveDocAsync(doc); ProcessLog.WriteEntry(url); } Message Pump UI Thread

14 Asynchronous Control Flow
async void DoWorkAsync() { var t1 = ProcessFeedAsync(" var t2 = ProcessFeedAsync(" await Task.WhenAll(t1, t2); DisplayMessage("Done"); } async Task ProcessFeedAsync(string url) { var text = await DownloadFeedAsync(url); var doc = ParseFeedIntoDoc(text); await SaveDocAsync(doc); ProcessLog.WriteEntry(url); }

15 Asynchronous Control Flow
async void DoWorkAsync() { var t1 = ProcessFeedAsync(" var t2 = ProcessFeedAsync(" await Task.WhenAll(t1, t2); DisplayMessage("Done"); } async Task ProcessFeedAsync(string url) { var text = await DownloadFeedAsync(url); var doc = ParseFeedIntoDoc(text); await SaveDocAsync(doc); ProcessLog.WriteEntry(url); }

16 Asynchronous Control Flow
async void DoWorkAsync() { var t1 = ProcessFeedAsync(" var t2 = ProcessFeedAsync(" await Task.WhenAll(t1, t2); DisplayMessage("Done"); } async Task ProcessFeedAsync(string url) { var text = await DownloadFeedAsync(url); var doc = ParseFeedIntoDoc(text); await SaveDocAsync(doc); ProcessLog.WriteEntry(url); }

17 Asynchronous Control Flow
async void DoWorkAsync() { var t1 = ProcessFeedAsync(" var t2 = ProcessFeedAsync(" await Task.WhenAll(t1, t2); DisplayMessage("Done"); } async Task ProcessFeedAsync(string url) { var text = await DownloadFeedAsync(url); var doc = ParseFeedIntoDoc(text); await SaveDocAsync(doc); ProcessLog.WriteEntry(url); } t1

18 Asynchronous Control Flow
async void DoWorkAsync() { var t1 = ProcessFeedAsync(" var t2 = ProcessFeedAsync(" await Task.WhenAll(t1, t2); DisplayMessage("Done"); } async Task ProcessFeedAsync(string url) { var text = await DownloadFeedAsync(url); var doc = ParseFeedIntoDoc(text); await SaveDocAsync(doc); ProcessLog.WriteEntry(url); } t1 t2

19 Asynchronous Control Flow
async void DoWorkAsync() { var t1 = ProcessFeedAsync(" var t2 = ProcessFeedAsync(" await Task.WhenAll(t1, t2); DisplayMessage("Done"); } async Task ProcessFeedAsync(string url) { var text = await DownloadFeedAsync(url); var doc = ParseFeedIntoDoc(text); await SaveDocAsync(doc); ProcessLog.WriteEntry(url); } t1 t2

20 Asynchronous Control Flow
async void DoWorkAsync() { var t1 = ProcessFeedAsync(" var t2 = ProcessFeedAsync(" await Task.WhenAll(t1, t2); DisplayMessage("Done"); } async Task ProcessFeedAsync(string url) { var text = await DownloadFeedAsync(url); var doc = ParseFeedIntoDoc(text); await SaveDocAsync(doc); ProcessLog.WriteEntry(url); } t1 t2

21 Asynchronous Control Flow
async void DoWorkAsync() { var t1 = ProcessFeedAsync(" var t2 = ProcessFeedAsync(" await Task.WhenAll(t1, t2); DisplayMessage("Done"); } async Task ProcessFeedAsync(string url) { var text = await DownloadFeedAsync(url); var doc = ParseFeedIntoDoc(text); await SaveDocAsync(doc); ProcessLog.WriteEntry(url); } t1 t2

22 Asynchronous Control Flow
async void DoWorkAsync() { var t1 = ProcessFeedAsync(" var t2 = ProcessFeedAsync(" await Task.WhenAll(t1, t2); DisplayMessage("Done"); } async Task ProcessFeedAsync(string url) { var text = await DownloadFeedAsync(url); var doc = ParseFeedIntoDoc(text); await SaveDocAsync(doc); ProcessLog.WriteEntry(url); } t1 t2

23 Asynchronous Control Flow
async void DoWorkAsync() { var t1 = ProcessFeedAsync(" var t2 = ProcessFeedAsync(" await Task.WhenAll(t1, t2); DisplayMessage("Done"); } async Task ProcessFeedAsync(string url) { var text = await DownloadFeedAsync(url); var doc = ParseFeedIntoDoc(text); await SaveDocAsync(doc); ProcessLog.WriteEntry(url); } t1 t2

24 Asynchronous Control Flow
async void DoWorkAsync() { var t1 = ProcessFeedAsync(" var t2 = ProcessFeedAsync(" await Task.WhenAll(t1, t2); DisplayMessage("Done"); } async Task ProcessFeedAsync(string url) { var text = await DownloadFeedAsync(url); var doc = ParseFeedIntoDoc(text); await SaveDocAsync(doc); ProcessLog.WriteEntry(url); } t1 t2

25 Asynchronous Control Flow
async void DoWorkAsync() { var t1 = ProcessFeedAsync(" var t2 = ProcessFeedAsync(" await Task.WhenAll(t1, t2); DisplayMessage("Done"); } async Task ProcessFeedAsync(string url) { var text = await DownloadFeedAsync(url); var doc = ParseFeedIntoDoc(text); await SaveDocAsync(doc); ProcessLog.WriteEntry(url); } t1 t2

26 Asynchronous Control Flow
async void DoWorkAsync() { var t1 = ProcessFeedAsync(" var t2 = ProcessFeedAsync(" await Task.WhenAll(t1, t2); DisplayMessage("Done"); } async Task ProcessFeedAsync(string url) { var text = await DownloadFeedAsync(url); var doc = ParseFeedIntoDoc(text); await SaveDocAsync(doc); ProcessLog.WriteEntry(url); } t1 t2

27 Asynchronous Control Flow
async void DoWorkAsync() { var t1 = ProcessFeedAsync(" var t2 = ProcessFeedAsync(" await Task.WhenAll(t1, t2); DisplayMessage("Done"); } async Task ProcessFeedAsync(string url) { var text = await DownloadFeedAsync(url); var doc = ParseFeedIntoDoc(text); await SaveDocAsync(doc); ProcessLog.WriteEntry(url); } t1 t2

28 Asynchronous Control Flow
async void DoWorkAsync() { var t1 = ProcessFeedAsync(" var t2 = ProcessFeedAsync(" await Task.WhenAll(t1, t2); DisplayMessage("Done"); } async Task ProcessFeedAsync(string url) { var text = await DownloadFeedAsync(url); var doc = ParseFeedIntoDoc(text); await SaveDocAsync(doc); ProcessLog.WriteEntry(url); } t1 t2

29 Asynchronous Control Flow
async void DoWorkAsync() { var t1 = ProcessFeedAsync(" var t2 = ProcessFeedAsync(" await Task.WhenAll(t1, t2); DisplayMessage("Done"); } async Task ProcessFeedAsync(string url) { var text = await DownloadFeedAsync(url); var doc = ParseFeedIntoDoc(text); await SaveDocAsync(doc); ProcessLog.WriteEntry(url); } t1 t2

30 What About Exceptions? Should be as simple as the synchronous case
var wc = new WebClient(); try {    string txt = await wc.DownloadStringTaskAsync(url);    dataTextBox.Text = txt; } catch(WebException x) {    // Handle as usual

31 How Does it Work? 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; }

32 How Does it Work? 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; 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; }

33 Asynchronous Methods As simple as synchronous code
Unifies computational, network and I/O asynchrony More scalable servers More responsive UI

34 C# Evolution C# v.Next Asynchronous Programming C# 4.0 Dynamic Typing
Language Integrated Query C# 2.0 Generics C# 1.0 Managed Code

35 Compiler as a Service Compiler Compiler Meta-programming
Class Field public Foo private string X Meta-programming Read-Eval-Print Loop Language Object Model DSL Embedding Compiler Compiler Source File .NET Assembly Source code Source code Source code Source code

36 Resources Visual Studio Async CTP Eric Lippert’s Blog
Eric Lippert’s Blog The Async CTP does not currently work if SP1 is installed Need to install on top of the RTM version A new CTP will probably be released soon that will work with SP1


Download ppt "Asynchronous Programming with C# v.Next"

Similar presentations


Ads by Google