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

前端 未结 5 552
佛祖请我去吃肉
佛祖请我去吃肉 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:11

    It is much more easy:

    static class Program
    {
      [ThreadStatic]
      public static readonly bool IsMainThread = true;
    
    //...
    }
    

    And you can use it from any thread:

    if(Program.IsMainThread) ...
    

提交回复
热议问题