I’m thinking of using this code to detect if the thread is the main (UI) thread.
public static bool IsMainThread()
{
return Thread.CurrentThread.GetApartmentState() == ApartmentState.STA;
}
It certainly appears to work fine with worker threads running tasks returning false, just wondering if it is generally true.
If it’s relevant, this is a WPF application.
This is similar to my other question: How do I assert that the code is running in the main thread? But no one suggested this solution.
Yes, you can set STA on other threads when you create them. Thread pool threads are in the MTA.