11 Controlling on which thread your asynchronous code runs

This chapter covers

  • The await threading behavior
  • Understanding SynchronizationContext
  • When to use ConfigureAwait
  • Using Task.Yield
  • The basics of TaskScheduler

Most of the time, you don’t care on which thread your code runs. If you calculate something, your calculation will produce the exact same result regardless of the thread or CPU core it runs on. But some operations do work differently, depending on the thread that runs them, the most common being

11.1 await-threading behavior

11.1.1 await in UI threads

11.1.2 await in non-UI threads

11.2 Synchronization contexts

11.3 Breaking away—ConfigureAwait(false)

11.4 More ConfigureAwait options

11.5 Letting other code run: Task.Yield

11.6 Task schedulers

Summary