Task-based Asynchronous Pattern - Introduction

With .Net 4.5 and async/await we have yet another pattern for asynchronous programming and it's time for you to really embrace this. The pattern is called Task-based Asynchronous Pattern, or TAP for short. in my opinion a good asynchronous pattern makes asynchronous code look synchronous. i think it's something with our brains that just have a hard time grasping asynchronously in a good way. But We have to wait and see if there will be a downside with TAP because a pattern that hides something too well might also cause problems, especially when it comes to edge cases.

Anyway, what is TAP? Well you use tasks and in my opinion you typically avoid using the ContinueWith method. The use of ContinueWith will probably be more to optimize than make the code easy to understand. Apart from that (my own observation) a TAP method follow the following rules:

  • Returns a running (a.k.a. hot) Task or Task<T>.
  • Has an Async suffix in the method name, except for task combinators such as "WhenAll".
  • Returns quickly to the caller.
  • Is overloaded to accept cancellation token and/or IProgress<T> if it supports cancellation/progress reporting.
  • Does not do blockingĀ I/O on the calling thread.

Over the next few days I'll give you some nifty task combinators you might want to use.