High-level notes on the SynchronizationContext

DRAFT DRAFT DRAFT DRAFT DRAFT DRAFT DRAFT DRAFT DRAFT

Questions to Answer

  1. How do we safely do a fire-and-forget with async/await?
  2. On what thread does the SynchronizationContext itself run?
  3. How do we access the SynchronizationContext ?
  4. How does the SynchronizationContext differ from the IAsyncStateMachine ?
  5. What state does the SynchronizationContext capture?

Historical Context

ISynchronizeInvoke standardizes use of the Windows Message Queue.

SynchronizationContext replaces it to support ASP.NET async pages.

SynchronizationContext Implementations

WindowsFormsSynchronizationContext & DispatcherSynchronizationContext

Default SynchronizationContext

AspNetSynchronizationContext

The Task Parallel Library (TPL)

async/await

Marshalling among Threads with Async/Await

Schedulers

Give a scheduler some work and the scheduler determines:

Some schedulers:

SynchronizationContext

TaskScheduler

ThreadPool

White Elephant

References

https://msdn.microsoft.com/magazine/gg598924.aspx

http://stackoverflow.com/questions/5600761/what-is-marshalling-what-is-happening-when-something-is-marshalled

https://blogs.msdn.microsoft.com/pfxteam/2012/01/20/await-synchronizationcontext-and-console-apps/

https://msdn.microsoft.com/library/system.threading.tasks.taskscheduler.aspx