210 likes | 369 Views
TOOL-810T. Async made simple in Windows 8 with C# and Visual Basic. Mads Torgersen and Alex Turner Program Managers Microsoft Corporation. The future is the future. .NET Async. Windows Runtime Async. Responsiveness and scalability Async is becoming the norm
E N D
TOOL-810T Async made simplein Windows 8with C# and Visual Basic Mads Torgersen and Alex Turner Program Managers Microsoft Corporation
The future is the future • .NET Async • Windows Runtime Async • Responsiveness and scalability • Async is becoming the norm • Futures: Modern abstractions • Details: Different but similar
Synchronous vs. asynchronous • var data = DownloadData(...); • ProcessData(data); STOP DownloadData ProcessData varfuture = DownloadDataAsync(...); future.ContinueWith(data => ProcessData(data)); DownloadDataAsync ProcessData
Synchronous vs. asynchronous • var data = DownloadData(...); • ProcessData(data); STOP STOP DownloadData ProcessData varfuture = DownloadDataAsync(...); future.ContinueWith(data => ProcessData(data)); DownloadDataAsync ProcessData
C# and Visual Basic let you do asynchronous programming without callbacks
demo Using await with theWindows Runtime
Awaitables IAsyncAction IAsyncOperation<TResult> • Returned from async Windows Runtime APIs • Must be started • awaitdirectly • StartAsTask and await later Task Task<TResult> • Returned from async C# and Visual Basic APIs • Already running • await directly • Store and awaitlater
Task returning vs void returning async void Foo_Click(…); • Cannot be awaited • “Fire and forget” • Start separate independent flow • Use for event handlers • Use to override void methods async Task FooAsync(…); • Can be awaited • “Give back control” • Delegate asynchronous work • Use for helper methods • Use for library methods
demo Coordinating Tasks
Task helpers • Yielding control await Task.Delay(5000); await Task.Yield(); • Background running var result = await Task.Run(() => { … work … }); • Parallel composition • Task first = await Task.WhenAny(task1, task2); • varresults = await Task.WhenAll(task1, task2);
demo Cancellation
Cancellation and Progress • Pass CancellationToken to send cancellation • Pass IProgess to receive progress updates await FooAsync(…, cancel, progress); await FooAsync(…).StartAsTask(cancel, progress);
Related sessions • [TOOL-531T] Using the Windows Runtime from C# and Visual Basic • [PLAT-203T] Async everywhere: creating responsive APIs & apps • [TOOL-816T] Future directions for C# and Visual Basic • [SAC-804T] Building IIS and ASP.NET apps with the power of async --- • [TOOL-829T] The zen of async: Best practices for best performance
Further reading and documentation • Async homepage: www.msdn.com/vstudio/async • What's New for Visual C#: http://msdn.com/en-us/library/hh156499(VS.110).aspx • What's New for Visual Basic:http://msdn.com/en-us/library/we86c8x2(VS.110).aspx
thank you Feedback and questions http://forums.dev.windows.com Session feedbackhttp://bldw.in/SessionFeedback
© 2011 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.