How to Use SynchronizationContext to Post Messages

This article describes how to use a SynchronizationContext to post messages.

While working with UI based applications, one of the most important things that you need to keep in mind isThread Affinity. It ensures that every control needs to be created in the UI Thread to be ported in the UI and each of them could only be accessed in the UI thread only. Earlier, Windows Forms applications uses the ISynchronizeInvoke interface to invoke code in its own thread. SynchronizationContext is a new threading model that allows you to easily communicate with other threading models to simplify the synchronous and asynchronous operations on it.

SynchronizationContext is a new class introduced recently and is being widely used in the .NET base class library itself (in async CTP for instance) and provides a new way of implementing the Threading Model for your application. It represents the abstract base class for a model that has two methods, namely Send (which invokes a delegate synchronously) and Post (which invokes a delegate asynchronously). Each of these methods take a delegate of type SendOrPostCallback and the state object. Each time the SynchronizationContext is created, it associates the current thread to its context, so based on the thread in which the block is executing, "SynchronizationContext.Current" always gets you the object that is created in the current thread.

Remember: The Post actually uses ThreadPool internally to call the callback you pass asynchronously.

Now let's create a class that uses SynchronizationContext:

  1. public class MyCallerType  
  2. {  
  3.     private Thread currentThread;  
  4.   
  5.     private SynchronizationContext context;  
  6.   
  7.     public event EventHandler EventCallback;  
  8.   
  9.     public MyCallerType()  
  10.     {  
  11.         context = SynchronizationContext.Current;  
  12.         context = context ?? new SynchronizationContext();  
  13.   
  14.         currentThread = new Thread(new ThreadStart(Run));  
  15.         currentThread.Start();  
  16.     }  
  17.   
  18.     private void CallEventHandler(object state)  
  19.     {  
  20.         EventHandler handler = EventCallback;  
  21.   
  22.         if (handler != null)  
  23.         {  
  24.             handler(this, EventArgs.Empty);  
  25.         }  
  26.     }  
  27.   
  28.     private void Run()  
  29.     {  
  30.         context.Send(new SendOrPostCallback(this.CallEventHandler), null);  
  31.     }  
  32.     private void RunAsync()  
  33.     {  
  34.         context.Post(new SendOrPostCallback(this.CallEventHandler), null);  
  35.     }  
  36. }  

The class MyCallerType actually holds the current SynchronizationContext type itself. So when Run orRunAsync is called, it gets the current thread form its SynchronizationContext (which is held from its object creation) and invokes it.

I hope you enjoyed this article. I will cover a bit more later.

Thank you for reading.