title: Threading issues in C# from external process
body: <p>I have this simple code:</p>
  
  <pre><code>public void Run()
  {
     var invokerThread = new Thread(new ThreadStart(RunOnBackground));
     invokerThread.Start();
  }
  
  private void RunOnBackground()
  {
     Trace.WriteLine("hi");
     ...
  }
  </code></pre>
  
  <p>Unfortunately when running this code (from third party process) the thread doesn't really run.
  Either in process explorer and in VS debugger I see that the thread is created and its state is "Running".</p>
  
  <p>The main thread's apartment is STA and I've tried both STA and MTA on internal thread.</p>
  
  <p>When I add to the <code>Run()</code> method at the end <code>invokerThread.Join();</code> then the thread does run. But then again it doesn't really help.</p>
  
  <p>What am I missing?</p>
  
  <p>Edit: Here is some more information regarding the code hosting -</p>
  
  <p><code>Run()</code> method is called via COM interop from a process which is also managed executable assembly (the reason COM interop is used is because all other components in the system are native). </p>
  
  <p>The method <code>RunOnBackground()</code> includes some more code after the tracing and generally its execution lasts between 10 - 20 seconds, including starting another process and waiting for its termination. Also I have some other areas in the code where I write some debug information to the Trace. While debugging the code, <code>Run()</code> runs as usual and after <code>invokerThread.Start();</code> invokerThread's state is "Running" (though breakpoints inside the <code>RunOnBackground()</code> method don't stop).</p>
  
  <p>When I add <code>invokerThread.Join()</code> at the end of the <code>Run()</code> method the debugger goes to <code>RunOnBackground()</code> <strong>after</strong> the <code>Join()</code>.</p>