How can I check if the current running thread is on the GUI context or not?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
It’s unfortunately hard to answer this question with 100% accuracy because it’s not always entirely obvious what constitutes a GUI Context. It’s more of a heuristic than a yes / no answer. And the heuristic will be different for every GUI framework.
For WPF a good one is to check and see if there as an active
Dispatcherfor the current threadHowever this can be fooled by just setting up a
Dispatcheron a random thread but not actually putting a GUI on top of it.For WinForms a good one to check is the current
SynchronizationContext.However this can be fooled by someone temporarily (or longer) resetting the
Currentvalue to another synchronization context. This is essentially just a global thread and can be set by anyone. It’s actually fairly common for it to change in certain applications like Visual Studio (but that’s a WPF app though)