What is SynchronizationContext in .NET

  • multi threaded programs usually uses one of the threads as means of synchronization
  • there are message loops to pass unit of works (like Windows message queue)
  • message queues require messages formats and convention for handling it
  • single thread to handle ASP.NET request is waste of the resources (before .NET 2)
  • thread pool was better but inconvenient to work with
  • instead SynchronizationContext queues work for given context not thread
  • thread have current context
  • context does not have to be unique but can be shared between multiple threads
  • thread can change context but this is rare
  • SynchronizationContext keeps count of all asynchronous operations
  • Windows Forms uses WindowsFormsSynchronizationContext
  • WPF and Silverlight uses DispatcherSynchronizationContext
  • Console app and WindowsServices and ThreadPool threads uses default SynchronizationContext
  • ASP.NET uses AspNetSynchronizationContext

Further read:

https://learn.microsoft.com/pl-pl/archive/msdn-magazine/2011/february/msdn-magazine-parallel-computing-it-s-all-about-the-synchronizationcontext

One Reply to “What is SynchronizationContext in .NET”

Leave a Reply

Your email address will not be published. Required fields are marked *

Solve : *
29 + 25 =