Why is SynchronizationContext.Current null in my Winforms application?

Ian Ringrose picture Ian Ringrose · Nov 10, 2009 · Viewed 18.2k times · Source

I just wrote this code:

System.Threading.SynchronizationContext.Current.Post(
    state => DoUpdateInUIThread((Abc)state), 
    abc);

but System.Threading.SynchronizationContext.Current is null

Answer

Ian Ringrose picture Ian Ringrose · Nov 10, 2009

To get it to work.

In your class

private SynchronizationContext synchronizationContext;

In the UI thread (main thread)

synchronizationContext = System.Threading.SynchronizationContext.Current;

In the worker thread

synchronizationContext.Post(    
   state => DoUpdateInUIThread((Abc)state),     
   abc);