Cross Thread Update UI using SynchronizationContext | .Net

Cross Thread Update UI using SynchronizationContext | .Net

Added by Nasir Mahmood updated on Saturday, February 10, 2018


Previously we discuss how to update UI Cross Thread Update UI | .Net

While working with UI based applications, one of the most important thing that you need to keep in mind is Thread 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 through UI Thread only. Earlier, the Windows Forms application uses ISynchronizeInvoke interface to invoke a code in its own Thread. SynchronizationContext is a new Threading model which allows you to easily communicate with other Threading models to simplify the Synchronous and asynchronous operation on it.


The SynchronizationContext class is a base class that provides a free-threaded context with no synchronization. The purpose of the synchronization model implemented by this class is to allow the internal asynchronous/synchronous operations of the common language runtime to behave properly with different synchronization models. This model also simplifies some of the requirements that managed applications have had to follow in order to work correctly under different synchronization environments.

How to Use?:

Define SynchronizationContext variable as show below.

private readonly SynchronizationContext _syncContext;				

Then in constructor of class assign it with SynchronizationContext.Current as shown below

_syncContext = SynchronizationContext.Current;				

Now call the following code anywhere where you want to update UI from other thread.

_syncContext.Post(o => 
	//Update UI here


32 Tutorials
27 Snippets
6 Products


Contact Us

Contact us

Stay Connected