Reliable way to check if current thread is the main thread in console and WPF applications #110141
Unanswered
alexey-leonovich
asked this question in
Q&A
Replies: 1 comment 3 replies
-
What do you need this check for? In other words, what is your definition of main thread? For example, what's the main thread if the .exe is native binary and the .NET runtime is used to run a COM component? Neither of the code snippets that you have shared above look reliable. The fully reliable way would be to have the caller of your code to set the main thread explicitly. |
Beta Was this translation helpful? Give feedback.
3 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I need to check if current thread is the main thread. I need to support both console and WPF applications - so I cannot check SynchronizationContext.Current or Application.Current.Dispatcher.Thread. An I need to support both .NFW 472 and .NET 8.0.
Thread.CurrentThread.ManagedThreadId
always equals 1?or is it an overkill and there is a simplier solution?
3) Or maybe some extra checks via reflection are needed:
Thank you.
Beta Was this translation helpful? Give feedback.
All reactions