Chapter 6. Async implementation

 

This chapter covers

  • The structure of asynchronous code
  • Interacting with the framework builder types
  • Performing a single step in an async method
  • Understanding execution context flow across await expressions
  • Interacting with custom task types

I vividly remember the evening of October 28, 2010. Anders Hejlsberg was presenting async/await at PDC, and shortly before his talk started, an avalanche of downloadable material was made available, including a draft of the changes to the C# specification, a Community Technology Preview (CTP) of the C# 5 compiler, and the slides Anders was presenting. At one point, I was watching the talk live and skimming through the slides while the CTP installed. By the time Anders had finished, I was writing async code and trying things out.

In the next few weeks, I started taking bits apart and looking at exactly what code the compiler was generating, trying to write my own simplistic implementation of the library that came with the CTP, and generally poking at it from every angle. As new versions came out, I worked out what had changed and became more and more comfortable with what was going on behind the scenes. The more I saw, the more I appreciated how much boilerplate code the compiler is happy to write on our behalf. It’s like looking at a beautiful flower under a microscope: the beauty is still there to be admired, but there’s so much more to it than can be seen at first glance.

6.1. Structure of the generated code

6.2. A simple MoveNext() implementation

6.3. How control flow affects MoveNext()

6.4. Execution contexts and flow

6.5. Custom task types revisited

Summary

sitemap