370 likes | 547 Views
Kevin Pilch-Bisson Dev Lead for C# and Visual Basic IDE Services Microsoft Corporation. The Future of C # and Visual Basic. C# and VB Evolution. C# + VB v.Next. Asynchronous Programming. C# 4.0 + VB 10.0. Dynamic + Language Parity. C# 3.0 + VB 9.0. Language Integrated Query.
E N D
Kevin Pilch-Bisson Dev Lead for C# and Visual Basic IDE Services Microsoft Corporation The Future of C# andVisual Basic
C# and VB Evolution C# + VB v.Next Asynchronous Programming C# 4.0 + VB 10.0 Dynamic + Language Parity C# 3.0 + VB 9.0 Language Integrated Query C# 2.0 + VB 8.0 Generics C# 1.0 + VB 7.0 Managed Code
Asynchronous Programming • 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
Asynchrony in a Nutshell • Synchronous Wait for result before returning • string DownloadString(...); • Asynchronous Return now, call back with result • void DownloadStringAsync(..., Action<string> callback); • Sharing Threads between activities • UI responsiveness: Free UI thread for user interaction • Server scalability: Reuse thread for other requests
Synchronous vs. Asynchronous var data = DownloadData(...); ProcessData(data); STOP Thread DownloadData ProcessData DownloadDataAsync(... , data => { ProcessData(data); }); Thread DownloadDataAsync ProcessData
Synchronous vs. Asynchronous var data = DownloadData(...); ProcessData(data); STOP STOP Thread DownloadData ProcessData DownloadDataAsync(... , data => { ProcessData(data); }); Thread DownloadDataAsync ProcessData
Visual Studio Async CTP http://msdn.com/vstudio/async Announcing
Asynchronous Methods • Goal: Just like synchronous programming • You can have your cake and eat it too! • Framework: Use Task<T> for all asynchrony • Add Task<T> support to existing .NET and Silverlight APIs • Easy for you to do the same • Languages: Asynchronous Methods • “async” modifier marks method or lambda as asynchronous • “await” operator yields control until awaited task completes
Task<T> • Represents “ongoing operation” • 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.
Asynchronous Control Flow async voidDoWorkAsync() { var t1 = ProcessFeedAsync("www.acme.com/rss"); var t2 = ProcessFeedAsync("www.xyznews.com/rss"); awaitTask.WhenAll(t1, t2); DisplayMessage("Done"); } asyncTaskProcessFeedAsync(stringurl) { var text = awaitDownloadFeedAsync(url); var doc = ParseFeedIntoDoc(text); awaitSaveDocAsync(doc); ProcessLog.WriteEntry(url); } MessagePump UI Thread
Asynchronous Control Flow async voidDoWorkAsync() { var t1 = ProcessFeedAsync("www.acme.com/rss"); var t2 = ProcessFeedAsync("www.xyznews.com/rss"); awaitTask.WhenAll(t1, t2); DisplayMessage("Done"); } asyncTaskProcessFeedAsync(stringurl) { var text = awaitDownloadFeedAsync(url); var doc = ParseFeedIntoDoc(text); awaitSaveDocAsync(doc); ProcessLog.WriteEntry(url); }
Asynchronous Control Flow async voidDoWorkAsync() { var t1 = ProcessFeedAsync("www.acme.com/rss"); var t2 = ProcessFeedAsync("www.xyznews.com/rss"); awaitTask.WhenAll(t1, t2); DisplayMessage("Done"); } asyncTaskProcessFeedAsync(stringurl) { var text = awaitDownloadFeedAsync(url); var doc = ParseFeedIntoDoc(text); awaitSaveDocAsync(doc); ProcessLog.WriteEntry(url); }
Asynchronous Control Flow async voidDoWorkAsync() { var t1 = ProcessFeedAsync("www.acme.com/rss"); var t2 = ProcessFeedAsync("www.xyznews.com/rss"); awaitTask.WhenAll(t1, t2); DisplayMessage("Done"); } asyncTaskProcessFeedAsync(stringurl) { var text = awaitDownloadFeedAsync(url); var doc = ParseFeedIntoDoc(text); awaitSaveDocAsync(doc); ProcessLog.WriteEntry(url); }
Asynchronous Control Flow async voidDoWorkAsync() { var t1 = ProcessFeedAsync("www.acme.com/rss"); var t2 = ProcessFeedAsync("www.xyznews.com/rss"); awaitTask.WhenAll(t1, t2); DisplayMessage("Done"); } asyncTaskProcessFeedAsync(stringurl) { var text = awaitDownloadFeedAsync(url); var doc = ParseFeedIntoDoc(text); awaitSaveDocAsync(doc); ProcessLog.WriteEntry(url); } t1
Asynchronous Control Flow async voidDoWorkAsync() { var t1 = ProcessFeedAsync("www.acme.com/rss"); var t2 = ProcessFeedAsync("www.xyznews.com/rss"); awaitTask.WhenAll(t1, t2); DisplayMessage("Done"); } asyncTaskProcessFeedAsync(stringurl) { var text = awaitDownloadFeedAsync(url); var doc = ParseFeedIntoDoc(text); awaitSaveDocAsync(doc); ProcessLog.WriteEntry(url); } t1 t2
Asynchronous Control Flow async voidDoWorkAsync() { var t1 = ProcessFeedAsync("www.acme.com/rss"); var t2 = ProcessFeedAsync("www.xyznews.com/rss"); awaitTask.WhenAll(t1, t2); DisplayMessage("Done"); } asyncTaskProcessFeedAsync(stringurl) { var text = awaitDownloadFeedAsync(url); var doc = ParseFeedIntoDoc(text); awaitSaveDocAsync(doc); ProcessLog.WriteEntry(url); } t1 t2
Asynchronous Control Flow async voidDoWorkAsync() { var t1 = ProcessFeedAsync("www.acme.com/rss"); var t2 = ProcessFeedAsync("www.xyznews.com/rss"); awaitTask.WhenAll(t1, t2); DisplayMessage("Done"); } asyncTaskProcessFeedAsync(stringurl) { var text = awaitDownloadFeedAsync(url); var doc = ParseFeedIntoDoc(text); awaitSaveDocAsync(doc); ProcessLog.WriteEntry(url); } t1 t2
Asynchronous Control Flow async voidDoWorkAsync() { var t1 = ProcessFeedAsync("www.acme.com/rss"); var t2 = ProcessFeedAsync("www.xyznews.com/rss"); awaitTask.WhenAll(t1, t2); DisplayMessage("Done"); } asyncTaskProcessFeedAsync(stringurl) { var text = awaitDownloadFeedAsync(url); var doc = ParseFeedIntoDoc(text); awaitSaveDocAsync(doc); ProcessLog.WriteEntry(url); } t1 t2
Asynchronous Control Flow async voidDoWorkAsync() { var t1 = ProcessFeedAsync("www.acme.com/rss"); var t2 = ProcessFeedAsync("www.xyznews.com/rss"); awaitTask.WhenAll(t1, t2); DisplayMessage("Done"); } asyncTaskProcessFeedAsync(stringurl) { var text = awaitDownloadFeedAsync(url); var doc = ParseFeedIntoDoc(text); awaitSaveDocAsync(doc); ProcessLog.WriteEntry(url); } t1 t2
Asynchronous Control Flow async voidDoWorkAsync() { var t1 = ProcessFeedAsync("www.acme.com/rss"); var t2 = ProcessFeedAsync("www.xyznews.com/rss"); awaitTask.WhenAll(t1, t2); DisplayMessage("Done"); } asyncTaskProcessFeedAsync(stringurl) { var text = awaitDownloadFeedAsync(url); var doc = ParseFeedIntoDoc(text); awaitSaveDocAsync(doc); ProcessLog.WriteEntry(url); } t1 t2
Asynchronous Control Flow async voidDoWorkAsync() { var t1 = ProcessFeedAsync("www.acme.com/rss"); var t2 = ProcessFeedAsync("www.xyznews.com/rss"); awaitTask.WhenAll(t1, t2); DisplayMessage("Done"); } asyncTaskProcessFeedAsync(stringurl) { var text = awaitDownloadFeedAsync(url); var doc = ParseFeedIntoDoc(text); awaitSaveDocAsync(doc); ProcessLog.WriteEntry(url); } t1 t2
Asynchronous Control Flow async voidDoWorkAsync() { var t1 = ProcessFeedAsync("www.acme.com/rss"); var t2 = ProcessFeedAsync("www.xyznews.com/rss"); awaitTask.WhenAll(t1, t2); DisplayMessage("Done"); } asyncTaskProcessFeedAsync(stringurl) { var text = awaitDownloadFeedAsync(url); var doc = ParseFeedIntoDoc(text); awaitSaveDocAsync(doc); ProcessLog.WriteEntry(url); } t1 t2
Asynchronous Control Flow async voidDoWorkAsync() { var t1 = ProcessFeedAsync("www.acme.com/rss"); var t2 = ProcessFeedAsync("www.xyznews.com/rss"); awaitTask.WhenAll(t1, t2); DisplayMessage("Done"); } asyncTaskProcessFeedAsync(stringurl) { var text = awaitDownloadFeedAsync(url); var doc = ParseFeedIntoDoc(text); awaitSaveDocAsync(doc); ProcessLog.WriteEntry(url); } t1 t2
Asynchronous Control Flow async voidDoWorkAsync() { var t1 = ProcessFeedAsync("www.acme.com/rss"); var t2 = ProcessFeedAsync("www.xyznews.com/rss"); awaitTask.WhenAll(t1, t2); DisplayMessage("Done"); } asyncTaskProcessFeedAsync(stringurl) { var text = awaitDownloadFeedAsync(url); var doc = ParseFeedIntoDoc(text); awaitSaveDocAsync(doc); ProcessLog.WriteEntry(url); } t1 t2
Asynchronous Control Flow async voidDoWorkAsync() { var t1 = ProcessFeedAsync("www.acme.com/rss"); var t2 = ProcessFeedAsync("www.xyznews.com/rss"); awaitTask.WhenAll(t1, t2); DisplayMessage("Done"); } asyncTaskProcessFeedAsync(stringurl) { var text = awaitDownloadFeedAsync(url); var doc = ParseFeedIntoDoc(text); awaitSaveDocAsync(doc); ProcessLog.WriteEntry(url); } t1 t2
Asynchronous Control Flow async voidDoWorkAsync() { var t1 = ProcessFeedAsync("www.acme.com/rss"); var t2 = ProcessFeedAsync("www.xyznews.com/rss"); awaitTask.WhenAll(t1, t2); DisplayMessage("Done"); } asyncTaskProcessFeedAsync(stringurl) { var text = awaitDownloadFeedAsync(url); var doc = ParseFeedIntoDoc(text); awaitSaveDocAsync(doc); ProcessLog.WriteEntry(url); } t1 t2
How Does it Work? async Task<XElement> GetRssAsync(stringurl) { var client = newWebClient(); var task = client.DownloadStringTaskAsync(url); var text = await task; var xml = XElement.Parse(text); return xml; }
How Does it Work? Task<XElement> GetRssAsync(stringurl) { var $builder = AsyncMethodBuilder<XElement>.Create(); var $state = 0; TaskAwaiter<string> $a1; Action $resume = delegate { try { if ($state == 1) goto L1; var client = newWebClient(); 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; }
Unifying Asynchrony Asynchronous Methods Task • An asynchronous scenario • Scrape YouTube for video links • Download two or more videos concurrently • Create a mashup from downloaded videos • Save the resulting video CPU Network I/O Composite
Unifying Asynchrony Task CPU Network I/O Composite try { string[] videoUrls = awaitScrapeYoutubeAsync(url); // Network-bound Task<Video> t1 = DownloadVideoAsync(videoUrls[0]);// Start two downloads Task<Video> t2 = DownloadVideoAsync(videoUrls[1]); Video[] vids = awaitTask.WhenAll(t1, t2); // Wait for both Video v = awaitMashupVideosAsync(vids[0], vids[1]); // CPU-bound awaitv.SaveAsync(textbox.Text); // IO-bound } catch (WebException ex) { ReportError(ex); }
Asynchronous Methods • More responsive UI, more scalable servers • Unifies computational, network and I/O asynchrony • No more callbacks! DownloadVisual Studio Async CTPtoday!
Update – Compiler as a Service Class Language Object Model Read-Eval-Print Loop public Foo Field DSL Embedding Metaprogramming string x private Source Code C# Compiler .Net Assemblies
http://msdn.com/vstudio/async http://twitter.com/Pilchie kevinpi@microsoft.com Questions?