Chapter 15. Asynchrony with async/await

 

This chapter covers

  • The fundamental aims of asynchrony
  • Writing async methods and delegates
  • Compiler transformations for async
  • The task-based asynchronous pattern
  • Asynchrony in WinRT

Asynchrony has been a thorn in the side of developers for years. It’s been known to be useful as a way of avoiding tying up a thread while waiting for some arbitrary task to complete, but it’s also been a pain in the neck to implement correctly.

Even within the .NET Framework (which is still relatively young in the grand scheme of things), we’ve had three different models to try to make things simpler:

  • The BeginFoo / EndFoo approach from .NET 1.x, using IAsyncResult and AsyncCallback to propagate results
  • The event-based asynchronous pattern from .NET 2.0, as implemented by BackgroundWorker and WebClient
  • The Task Parallel Library (TPL) introduced in .NET 4 and expanded in .NET 4.5

Despite its generally excellent design, writing robust and readable asynchronous code with the TPL was hard. Although the support for parallelism was great, there are some aspects of general asynchrony that are much better fixed in a language instead of purely in libraries.

15.1. Introducing asynchronous functions

15.2. Thinking about asynchrony

15.3. Syntax and semantics

15.4. Asynchronous anonymous functions

15.5. Implementation details: compiler transformation

15.6. Using async/await effectively

15.7. Summary

sitemap