1 / 24

Async’ing Your Way to a Successful App with .NET

Async’ing Your Way to a Successful App with .NET. Stephen Toub Visual Studio 3-301. Agenda. 6 Tips for Building Better Apps. #1. Wrap Events with Tasks to Improve Control Flow. public async Task RunStoryboardAsync ( this Storyboard sb ) {

hina
Télécharger la présentation

Async’ing Your Way to a Successful App with .NET

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Async’ing Your Way to a Successful App with .NET Stephen Toub Visual Studio 3-301

  2. Agenda • 6Tips for Building Better Apps

  3. #1 Wrap Events with Tasks to Improve Control Flow publicasyncTaskRunStoryboardAsync(thisStoryboardsb) { vartcs = newTaskCompletionSource<bool>(); EventHandler<object> handler = (s,e) => tcs.SetResult(true); sb.Completed += handler; sb.Begin(); awaittcs.Task; sb.Completed -= handler; } publicTask<T> RunAsync(Func<T> func) { vartcs = newTaskCompletionSource<T>(); ThreadPool.QueueUserWorkItem(delegate { try { tcs.SetResult(func()); } catch (Exception e) { tcs.SetException(e); } }); returntcs.Task; } public Task Delay(int milliseconds) { vartcs = newTaskCompletionSource<bool>(); new Timer(_ => tcs.SetResult(true)).Change(milliseconds, -1); returntcs.Task; } publicasyncTaskWaitForClickAsync(thisButton button) { vartcs = newTaskCompletionSource<bool>(); RoutedEventHandlerhandler = (s,e) => tcs.SetResult(true); button.Click += handler; awaittcs.Task; button.Click -= handler; } publicTask<T> FooAsync() { vartcs = newTaskCompletionSource<T>(); // schedule something asynchronously // to invoke tcs.Set* returntcs.Task; } Library Support TaskCompletionSource<T> Can be used to wrap arbitrary operations as a Task Once operations are exposed as Tasks, you can compose over them as you would any Task Pattern

  4. Wrapping Events with Tasks to Improve Control Flow • Awaiting tasks makes it easy to compose sequences or graphs of operations.

  5. #2 Use Cancellation to Improve Responsiveness publicasyncTaskFooAsync(CancellationTokencancellationToken) { … awaitSomeDotNetAsync(cancellationToken); awaitSomeWinRTAsync().AsTask(cancellationToken); … } publicasyncTaskFooAsync() { … awaitSomeDotNetAsync(); awaitSomeWinRTAsync(); … } Library Support CancellationTokenas a composable cancellation mechanism varcts = new CancellationTokenSource(); SomeMethod(cts.Token); cts.Cancel(); Pattern

  6. Using Cancellation to Improve Responsiveness • Responsiveness isn’t just about keeping the UI available for interaction. More broadly, it’s about doing what the user wants when the user wants.

  7. #3 Be Wary of “Async Void” Language Support asyncvoid, async Task, & async Task<T> async Task & async Task<T> return handles to the outstanding work asyncvoid is a “fire-and-forget” mechanism Guidance Use async void methods only for top-level entry points (e.g. UI event handlers) (Use and await asyncTask-returning methods everywhere else) Avoid passing async lambdas to void-returning delegates (Don’t pass async lambdas to WinRT methods unless you really know what you’re doing)

  8. Avoiding “async void” • Many problems in async code can be traced back to using “async void” incorrectly.

  9. #4 Use ConfigureAwait for Perf & Responsiveness await task; // force continuation back to the current sync context awaittask.ConfigureAwait(false); // try to continue executing where awaited task completes SynchronizationContext represents a target for work DispatcherSynchronizationContext (Post: Dispatcher.BeginInvoke) WinRTSynchronizationContext (Post: CoreDispatcher.RunAsync) Await Behavior Guidance Use ConfigureAwait(false) on all awaits in all methods with code that’s context agnostic (which is most libraries)

  10. Using ConfigureAwait async void button1_Click(…) { await DoWorkAsync(); } async void button1_Click(…) { DoWorkAsync().Wait(); } asyncTaskDoWorkAsync() { awaitTask.Run(…); Console.WriteLine("Done task"); } asyncTaskDoWorkAsync() { awaitTask.Run(…).ConfigureAwait(false); Console.WriteLine("Done task"); } Use ConfigureAwait(false) 2. Task.Run schedules work to run on thread pool 1. DoWorkAsync invoked on UI thread 3. Await captures SynchronizationContext and hooks up a continuation to run when task completes 4. UI blocks waiting for DoWorkAsync-returned Task to complete 5. Task.Run task completes on pool & invokes continuation which Posts back to UI thread 6. UI thread still blocked waiting for async operation to complete. Deadlock! .ConfigureAwait(false) avoids deadlock.

  11. Using ConfigureAwait for Responsiveness & Perf • Avoiding using the UI thread unless you actually need to.

  12. #5 Avoid “Sync over Async”, “Async over Sync” in Libraries

  13. What does asynchrony really mean? Synchronous Perform something here and now. I’ll regain control to execute something else when it’s done. Asynchronous Initiate something here and now. I’ll regain control to execute something else “immediately”.

  14. Sync vs Async “Pause for 10 seconds, then output 'Hello' to the console.” Synchronous Asynchronous publicstatic voidPausePrintAsync(){ThreadPool.QueueUserWorkItem(_ => PausePrint());} publicstatic TaskPausePrintAsync(){ return Task.Run(() => PausePrint());} publicstatic voidPausePrint(){var end = DateTime.Now + TimeSpan.FromSeconds(10);while(DateTime.Now < end);Console.WriteLine("Hello");} Possible scalability problems “async over sync” “syncover async” publicstatic TaskPausePrintAsync(){vartcs = newTaskCompletionSource<bool>(); newTimer(_ => {Console.WriteLine("Hello");tcs.SetResult(true); }).Change(10000, Timeout.Infinite);returntcs.Task;} publicstatic voidPausePrint(){Task t = PausePrintAsync();t.Wait();} publicstatic asyncTaskPausePrintAsync(){awaitTask.Delay(10000);Console.WriteLine("Hello");} Possible responsiveness problems

  15. #5 Avoid “Sync over Async”, “Async over Sync” in Libraries Guidance Be honest! Suffix should help caller to understand implementation. Define “FooAsync” iffyou’re not thread-bound (with a few notable exceptions). Don’t just expose FooAsyncas “return Task.Run(() => Foo());” Define “Foo” iffyou have a faster sync implementation that won’t deadlock. Don’t just expose Foo as “FooAsync().Wait();” Exceptions Methods exposed from .WinMD assemblies Overrides in some class hierarchies

  16. #6 Use Visual Studio’s async tooling Visual Studio 2012: Stepping with Async Step In Step Over Step Out

  17. Stepping in, over, and out with async • Stepping through your code as if it were synchronous.

  18. #7 Debugging Async in Visual Studio 2013 Visual Studio 2013: Call Stack Window Provides a “call stack” across async points. Visual Studio 2013: Tasks Window See and navigate from a list all active async operations.

  19. Using the Call Stack and Task Windows with async • Knowing how you got here and what’s going on

  20. Summary • 6Tips for Building Better Apps • Wrap events with tasks to improve control flow • Use cancellation to improve responsiveness • Be wary of “async void” • Use ConfigureAwait to improve perf & responsiveness • Avoid exposing “sync over async” or “async over sync” • Use Visual Studio’s async tooling Questions? Comments?

  21. Resources • Parallel Programming in .NET Blog: • http://blogs.msdn.com/pfxteam • Task-based Async Pattern Whitepaper • http://aka.ms/tap

  22. Required Slide *delete this box when your slide is finalized Your MS Tag will be inserted here during the final scrub. Evaluate this session • Scan this QR codeto evaluate this session and be automatically entered in a drawing to win a prize!

More Related