title: How to tell if a thread is the main thread in C#
body: <p>There are other posts that say you can create a control in windows forms and then check the <code>InvokeRequired</code> property to see if the current thread is the main thread or not.</p>
  <p>The problem is that you have no way of knowing if that control itself was created on the main thread.</p>
  <p>I am using the following code to tell if a thread is the main thread (the thread that started the process):</p>
  <pre><code>if (Thread.CurrentThread.GetApartmentState() != ApartmentState.STA ||
      Thread.CurrentThread.ManagedThreadId != 1 ||
      Thread.CurrentThread.IsBackground || Thread.CurrentThread.IsThreadPoolThread)
  {
      // not the main thread
  }
  </code></pre>
  <p>Does anyone know a better way? It seems like this way might be prone to errors or break in future versions of the runtime.</p>