Why is SynchronizationContext.Current null in my W

2019-01-25 03:40发布

I just wrote this code:

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

but System.Threading.SynchronizationContext.Current is null

2条回答
孤傲高冷的网名
2楼-- · 2019-01-25 04:04

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.

查看更多
【Aperson】
3楼-- · 2019-01-25 04:09

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);
查看更多
登录 后发表回答