Save 37% off PRO during our Black Friday Sale! »

Asynchronous Programming Tips and Tricks in .NET

Asynchronous Programming Tips and Tricks in .NET

3a53a5136f530c121a196914aecf3936?s=128

Tugberk Ugurlu

April 13, 2015
Tweet

Transcript

  1. Asynchronous Programming Tips and Tricks in .NET Tugberk Ugurlu tugberkugurlu.com

    @tourismgeek
  2. 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
  3. Assumptions • Knowledge on C# (duh!) • Prior Knowledge on

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

    • Event-based Asynchronous Pattern (EAP) • Task-Based Asynchronous Pattern (TAP)
  6. Asynchronous Programming Model (APM)

  7. Event-based Asynchronous Pattern (EAP)

  8. Task-Based Asynchronous Pattern (TAP)

  9. async/await and its glory

  10. 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
  11. Make Use of Pre Completed Tasks

  12. Make Use of Pre Completed Tasks DON’T

  13. Make Use of Pre Completed Tasks DON’T

  14. Make Use of Pre Completed Tasks DO

  15. Make Use of Pre Completed Tasks DO

  16. DON’T await on Task.FromResult

  17. DON’T await on Task.FromResult DON’T

  18. 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”
  19. Directly Return the Task DON’T

  20. Directly Return the Task DO

  21. 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
  22. Don’t Wrap Thread Offloading Work DON’T

  23. ConfigureAwait(false) http://channel9.msdn.com/Events/BUILD/BUILD2011/TOOL-829T

  24. ConfigureAwait(false) General rule of thumb: “When you are awaiting inside

    a library code, you should never care about SynchronizationContext and call ConfigureAwait(false)”
  25. ConfigureAwait(false) DON’T

  26. Task.WhenAll(Task[])

  27. 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”
  28. 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
  29. Resources • Slides: http://bit.ly/1CHNJzT • Source Code: https://github.com/tugberkugurlu/AsyncTips