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

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

    In my experience, if you attempt to create a dialog from another thread other than the main thread then windows gets all confused and things start going crazy. I tried to do this once with a status window to show the status of the background thread (as well as many other times someone would toss up a dialog from a background thread - and one that did have a Message Loop) - and Windows just started doing "random" things in the program. I'm pretty sure there was some unsafe handling of something going on. Had issues with clicking on the form and the wrong thread handling the messages...

    So, I would never have any UI coming up from Anywhere but the main thread.

    However, Why not simply save off the CurrentThread when you start, and compare the ThreadID with the current thread?

    -Chert

提交回复
热议问题