Students to Business Day 2012: Alex Turner

43

Transcript of Students to Business Day 2012: Alex Turner

Page 1: Students to Business Day 2012: Alex Turner
Page 2: Students to Business Day 2012: Alex Turner

C# and Visual Basic Future

Async Made Simple Alex

TurnerProgram ManagerVB/C# LanguagesMicrosoft

Page 3: Students to Business Day 2012: Alex Turner

Session Contents• Goals:• Learn about Async, the new language feature in VB and

C#• See how Async makes connected programs far simpler

to write

• Asynchronous programming is now pervasive…• The Async language feature makes it easy!• No more callbacks!

Page 4: Students to Business Day 2012: Alex Turner

C# and VB Evolution

Managed Code

Generics

Language Integrated Query

Dynamic + Language Parity

C# 1.0 + VB 7.0

C# 2.0 + VB 8.0

C# 3.0 + VB 9.0

C# 4.0 + VB 10.0

Page 5: Students to Business Day 2012: Alex Turner

Trends

Declarative

ConcurrentDynamic

Page 6: Students to Business Day 2012: Alex Turner

Trends• Increasingly connected applications• 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

Page 7: Students to Business Day 2012: Alex Turner

C# and VB Evolution

Managed Code

Generics

Language Integrated Query

Dynamic + Language Parity

C# 5.0 + VB 11.0Asynchronous Programming

C# 1.0 + VB 7.0

C# 2.0 + VB 8.0

C# 3.0 + VB 9.0

C# 4.0 + VB 10.0

Page 8: Students to Business Day 2012: Alex Turner

Asynchrony in a Nutshell• Synchronous Wait for result before returning• string DownloadString(...);

• Asynchronous Return now, call back with result• void DownloadStringAsync(..., Action<string> callback);

• Asynchrony benefits• UI responsiveness: Frees UI thread for interaction• Server scalability: Thread can be reused for other requests

Page 9: Students to Business Day 2012: Alex Turner

Synchronous versus AsynchronousThread

var data = DownloadData(...);ProcessData(data);

DownloadDataAsync(... , data => { ProcessData(data);});

Thread

DownloadDataAsync

ProcessData

STOP

ProcessData

DownloadData

Page 10: Students to Business Day 2012: Alex Turner

Synchronous versus AsynchronousThread

var data = DownloadData(...);ProcessData(data);

DownloadDataAsync(... , data => { ProcessData(data);});

Thread

DownloadDataAsync

ProcessData

STOP

ProcessData

DownloadData

STOP

Page 11: Students to Business Day 2012: Alex Turner

DemoAsynchronous Responsive UI

Page 12: Students to Business Day 2012: Alex Turner

Asynchronous Control Flowasync void DoWorkAsync() { var t1 = ProcessFeedAsync("www.acme.com/rss"); var t2 = ProcessFeedAsync("www.xyznews.com/rss"); 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);}

UI Thread

Message Pump

Page 13: Students to Business Day 2012: Alex Turner

Asynchronous Control Flowasync void DoWorkAsync() { var t1 = ProcessFeedAsync("www.acme.com/rss"); var t2 = ProcessFeedAsync("www.xyznews.com/rss"); 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);}

Page 14: Students to Business Day 2012: Alex Turner

Asynchronous Control Flowasync void DoWorkAsync() { var t1 = ProcessFeedAsync("www.acme.com/rss"); var t2 = ProcessFeedAsync("www.xyznews.com/rss"); 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);}

Page 15: Students to Business Day 2012: Alex Turner

Asynchronous Control Flowasync void DoWorkAsync() { var t1 = ProcessFeedAsync("www.acme.com/rss"); var t2 = ProcessFeedAsync("www.xyznews.com/rss"); 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);}

Page 16: Students to Business Day 2012: Alex Turner

Asynchronous Control Flowasync void DoWorkAsync() { var t1 = ProcessFeedAsync("www.acme.com/rss"); var t2 = ProcessFeedAsync("www.xyznews.com/rss"); 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

Page 17: Students to Business Day 2012: Alex Turner

Asynchronous Control Flowasync void DoWorkAsync() { var t1 = ProcessFeedAsync("www.acme.com/rss"); var t2 = ProcessFeedAsync("www.xyznews.com/rss"); 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

Page 18: Students to Business Day 2012: Alex Turner

Asynchronous Control Flowasync void DoWorkAsync() { var t1 = ProcessFeedAsync("www.acme.com/rss"); var t2 = ProcessFeedAsync("www.xyznews.com/rss"); 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

Page 19: Students to Business Day 2012: Alex Turner

Asynchronous Control Flowasync void DoWorkAsync() { var t1 = ProcessFeedAsync("www.acme.com/rss"); var t2 = ProcessFeedAsync("www.xyznews.com/rss"); 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

Page 20: Students to Business Day 2012: Alex Turner

Asynchronous Control Flowasync void DoWorkAsync() { var t1 = ProcessFeedAsync("www.acme.com/rss"); var t2 = ProcessFeedAsync("www.xyznews.com/rss"); 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

Page 21: Students to Business Day 2012: Alex Turner

Asynchronous Control Flowasync void DoWorkAsync() { var t1 = ProcessFeedAsync("www.acme.com/rss"); var t2 = ProcessFeedAsync("www.xyznews.com/rss"); 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

Page 22: Students to Business Day 2012: Alex Turner

Asynchronous Control Flowasync void DoWorkAsync() { var t1 = ProcessFeedAsync("www.acme.com/rss"); var t2 = ProcessFeedAsync("www.xyznews.com/rss"); 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

Page 23: Students to Business Day 2012: Alex Turner

Asynchronous Control Flowasync void DoWorkAsync() { var t1 = ProcessFeedAsync("www.acme.com/rss"); var t2 = ProcessFeedAsync("www.xyznews.com/rss"); 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

Page 24: Students to Business Day 2012: Alex Turner

Asynchronous Control Flowasync void DoWorkAsync() { var t1 = ProcessFeedAsync("www.acme.com/rss"); var t2 = ProcessFeedAsync("www.xyznews.com/rss"); 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

Page 25: Students to Business Day 2012: Alex Turner

Asynchronous Control Flowasync void DoWorkAsync() { var t1 = ProcessFeedAsync("www.acme.com/rss"); var t2 = ProcessFeedAsync("www.xyznews.com/rss"); 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

Page 26: Students to Business Day 2012: Alex Turner

Asynchronous Control Flowasync void DoWorkAsync() { var t1 = ProcessFeedAsync("www.acme.com/rss"); var t2 = ProcessFeedAsync("www.xyznews.com/rss"); 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

Page 27: Students to Business Day 2012: Alex Turner

Asynchronous Control Flowasync void DoWorkAsync() { var t1 = ProcessFeedAsync("www.acme.com/rss"); var t2 = ProcessFeedAsync("www.xyznews.com/rss"); 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

Page 28: Students to Business Day 2012: Alex Turner

Asynchronous Control Flowasync void DoWorkAsync() { var t1 = ProcessFeedAsync("www.acme.com/rss"); var t2 = ProcessFeedAsync("www.xyznews.com/rss"); 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

Page 29: Students to Business Day 2012: Alex Turner

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;}

Page 30: Students to Business Day 2012: Alex Turner

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;}

How Does it Work?

Task<XElement> GetRssAsync(string url) { var client = new WebClient(); var task = client.DownloadStringTaskAsync(url); return task.ContinueWith(delegate { var text = task.Result; var xml = XElement.Parse(text); return xml; });}

Page 31: Students to Business Day 2012: Alex Turner

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;}

How Does it Work?Task<XElement> GetRssAsync(string url) { var $builder = AsyncTaskMethodBuilder<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); $a1 = task.GetAwaiter(); if ($a1.IsCompleted) goto L1; $state = 1; $a1.OnCompleted($resume); return; L1: var text = $a1.GetResult(); var xml = XElement.Parse(text); $builder.SetResult(xml); } catch (Exception $ex) { $builder.SetException($ex); } }; $resume(); return $builder.Task;}

Page 32: Students to Business Day 2012: Alex Turner

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;}

How Does it Work?Task<XElement> GetRssAsync(string url) { var $builder = AsyncTaskMethodBuilder<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); $a1 = task.GetAwaiter(); if ($a1.IsCompleted) goto L1; $state = 1; $a1.OnCompleted($resume); return; L1: var text = $a1.GetResult(); var xml = XElement.Parse(text); $builder.SetResult(xml); } catch (Exception $ex) { $builder.SetException($ex); } }; $resume(); return $builder.Task;}

Page 33: Students to Business Day 2012: Alex Turner

Support throughout the .NET Framework

Others…

WindowsPresentationFoundation

ASP.NET

TaskParallelLibrary

WindowsCommunicationFoundation

ReactiveExtensions(Rx)

BaseClassLibrary

Others…

Page 34: Students to Business Day 2012: Alex Turner

Unifying Asynchrony

• An asynchronous scenario• Scrape YouTube for video links• Download two or more videos concurrently• Create a mashup from downloaded videos• Save the resulting video

Task

CPU Network I/O Composite

Asynchronous methods

Page 35: Students to Business Day 2012: Alex Turner

Unifying Asynchrony

Task

CPU Network I/O Composite

Asynchronous methods

try { string[] videoUrls = await ScrapeYoutubeAsync(url); // Network-bound Task<Video> t1 = DownloadVideoAsync(videoUrls[0]); // Start two downloads Task<Video> t2 = DownloadVideoAsync(videoUrls[1]); Video[] vids = await Task.WhenAll(t1, t2); // Wait for both Video v = await MashupVideosAsync(vids[0], vids[1]); // CPU-bound await v.SaveAsync(textbox.Text); // IO-bound}catch (WebException ex) { ReportError(ex);}

Page 36: Students to Business Day 2012: Alex Turner

Asynchronous Methods• As simple as synchronous code

• Unifies computational, network and I/O asynchrony

• More scalable servers

• More responsive UI Download

Visual Studio

Async CTP

today!

DownloadVisual Studio 11 Developer Previewtoday!

Page 37: Students to Business Day 2012: Alex Turner

C# and VB Evolution

Managed Code

Generics

Language Integrated Query

Dynamic + Language Parity

C# 5.0 + VB 11.0

Asynchronous Programming

C# 1.0 + VB 7.0

C# 2.0 + VB 8.0

C# 3.0 + VB 9.0

C# 4.0 + VB 10.0

Page 38: Students to Business Day 2012: Alex Turner

Class

Field

public Foo

private

string

X

Project Roslyn

CompilerCompilerSource codeSource code

SourceFile

Source codeSource code

.NET Assembly

Meta-programming Read-Eval-Print Loop

LanguageObject Model

DSL Embedding

Page 39: Students to Business Day 2012: Alex Turner

DemoProject Roslyn

Download

Microsoft

“Roslyn” CTP

today!

Page 40: Students to Business Day 2012: Alex Turner

Be what’s next• Find everything here

http://aka.ms/mbl-tech• Visual Studio Developer Preview Downloads

http://aka.ms/mbl-tech/devprev• MSDN HTML5 Developer Center

http://aka.ms/mbl-tech/html5

Page 41: Students to Business Day 2012: Alex Turner

Be what’s next

Visual Studio Async CTP http://msdn.com/async

Page 42: Students to Business Day 2012: Alex Turner

Be what’s next

Microsoft “Roslyn” CTP http://msdn.com/roslyn

Page 43: Students to Business Day 2012: Alex Turner

© 2012 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.