Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Asynchronous Programming Tips and Tricks in .NET

Asynchronous Programming Tips and Tricks in .NET

Tugberk Ugurlu

April 13, 2015
Tweet

More Decks by Tugberk Ugurlu

Other Decks in Programming

Transcript

  1. Agenda • Refresh on Asynchronous Programming Models on .NET •

    High level overview of async/await keywords • Tips and Tricks on TPL + async/await usage and scenarios
  2. Assumptions • Knowledge on C# (duh!) • Prior Knowledge on

    Asynchronous Programming • Prior Knowledge on TPL (Task Parallel Library)
  3. Asynchronous Programming Models in .NET • Asynchronous Programming Model (APM)

    • Event-based Asynchronous Pattern (EAP) • Task-Based Asynchronous Pattern (TAP)
  4. async/await and its glory For more information, check out: "Easy

    Asynchrony with C#: No More Callbacks! (http://bit.ly/1FEoK85)" talk by Mads Torgersen
  5. Directly Return the Task “Generally, if you are not going

    to be running any continuation in a method which consumes a Task returning code, don’t await on the Task”
  6. Don’t Wrap Thread Offloading Work “Library methods should not lie.

    If it looks async in its signature, it should be async in its implementation.” - Lucian Wischik https://channel9.msdn.com/Series/Three-Essential-Tips-for-Async/Async-Library-Methods-Shouldn-t-Lie
  7. ConfigureAwait(false) General rule of thumb: “When you are awaiting inside

    a library code, you should never care about SynchronizationContext and call ConfigureAwait(false)”
  8. Task.WhenAll(Task[]) “Be careful on how you do parallelization on server,

    refer to "How and Where Concurrent Asynchronous I/O with ASP.NET Web API (http://bit.ly/1NwGp5I)" for more information about this”
  9. There are MORE! • Six Essential Tips for Async: http://bit.ly/1ynpoEm

    • Async best practices: http://bit.ly/1DsFuMi • Async Performance: http://bit.ly/1EqWYdy • My Async Blog Posts: http://tugberkugurlu.com/tags/async • The zen of async: http://bit.ly/1NyIdvb