Why is SynchronizationContext.Current null in my Winforms application?

后端 未结 2 363
我在风中等你
我在风中等你 2020-12-17 09:17

I just wrote this code:

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

bu

相关标签:
2条回答
  • 2020-12-17 10:06

    See this explanation.

    SynchronizationContext.Current is only set in the main thread (which is the only thread where you don't actually need it)

    The blog post proposes a workaround.

    0 讨论(0)
  • 2020-12-17 10:13

    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);
    
    0 讨论(0)
提交回复
热议问题