async await best practices

aysnc await is probably one of the most important features of C#. It has made the life of developers easy. It helps developers to write clean code without any callbacks which are messy and difficult to understand.

However, if used in incorrectly async await it can cause havoc. It can lead to performance issues, deadlocks which are hard to debug. I have burnt hands due to incorrect use of  async await in the past and based on my little experience I can tell these issues will make your life hell, you will start questioning your very existence on this earth or why you chose to be a developer 🙂

I have tried to add common pitfalls while using async await  below. These are some of my learnings while working on problems that arise due to incorrect use of async await Much of these is inspired from Stephen Cleary blogs and Lucian Wischik six essential tips for Async channel 9 videos.

Here are tips:

  • AVOID using Task.Result or Task.Wait(). They make the calls synchronous and block async code.
  • Make your calls async all the way.
  • USE `Task.Delay` instead of `Thread.Sleep`
  • Understand the difference between CPU bound and IO bound operation before using Task Parallel Library or TPL
  • USE Task.Run or Parallel.ForEach for CPU bound operation.
  • USE await for IO bound operation.
  • USE ConfigureAwait(false)on the web APIs or library code. On the WPF application do not use ConfigureAwait(false) at the top level methods.
  • AVOID using `Task.Factory.StartNew`. Use Task.Run
  • DO NOT expose a synchronous method as asynchronous or visa vesra. In other words your library method should expose the true nature of method.
  • DO NOT use async void other than top level Events. ALWAYS return async Task

I hope these tips help few of you and help avoid common mistakes. Please suggest any other tips in the comments section.

Advertisements