How to tell if a thread is the main thread in C#

前端 未结 5 550
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-04 16:50

I know there are other posts that say you can create a control and then check the InvokeRequired property to see if the current thread is the main thread or not

5条回答
  •  鱼传尺愫
    2020-12-04 17:01

    If you're using Windows Forms or WPF, you can check to see if SynchronizationContext.Current is not null.

    The main thread will get a valid SynchronizationContext set to the current context upon startup in Windows Forms and WPF.

提交回复
热议问题