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. The problem is that you have no way of knowing if that control itself was created on the main thread.
I am using the following code to tell if a thread is the main thread (the thread that started the process):
if (Thread.CurrentThread.GetApartmentState() != ApartmentState.STA ||
Thread.CurrentThread.ManagedThreadId != 1 ||
Thread.CurrentThread.IsBackground || Thread.CurrentThread.IsThreadPoolThread)
{
// not the main thread
}
Does anyone know a better way? It seems like this way might be prone to errors or break in future versions of the runtime...