Episode

Six Essential Tips For Async - Introduction

Async involves some new concepts. They're not difficult; just unfamiliar. Over the past year Lucian Wischik has been watching how people use async/await in C# and VB. This series distils out the three top async patterns and anti-patterns, and top three tips for writing your own async libraries.

Tips:

  1. Async void is for top-level event-handlers only, and event-like things. Don't use it elsewhere in your code.
  2. It's crucial to distinguish CPU-bound work (should be done on threadpool) from IO-bound work (which needn't).
  3. You can wrap events up in Task-returning APIs and await them. This can dramatically simplify code.
  4. Async library methods shouldn't lie: they shouldn't use Task.Run internally in secret, and should expose an async signature if and only if their implementation is asynchronous
  5. Async library methods should have chunky async APIs not chatty. But if chatty is unavoidable, know about these micro-optimizations to reduce heap allocation overheads.
  6. Async library methods should consider task.ConfigureAwait(false) to reduce their impact on the UI message queue.

If you understand the basic flow of control in an async method, then those three points all fall naturally into place. This first introduction video explains that control flow.

Slides and source code are available on Lucian's blog.

C#