title: Thread.Join() hangs intermittently after last line is passed in thread
body: <p>I have console .NET application that is intended for execution of test automation.</p>
  
  <p>Application invokes a separate thread from the main thread and in that new thread executes automated script - as follows:</p>
  
  <pre><code>void runScriptSeparateThread(TestScript script)
  {
      // do some stuff
  
      Thread runScriptThread = new Thread(() =&gt; executeScript(script));
      runScriptThread.SetApartmentState(ApartmentState.STA);
      runScriptThread.Start();
  
      if (runScriptThread.Join(timeout) == false)
      {
          runScriptThread.Abort();
          File.AppendAllText(@"C:\log.txt", "Error: timeout ");
      }
      else
      {
          File.AppendAllText(@"C:\log.txt", "Message outer");
      }
  
      // do some other stuff
  }
  
  void executeScript(TestScript script)
  {
      // run test script using reflection calls to external assemblies
      // includes invocation of new threads which will live after this thread finishes
      // can potentially include any calls - according to needs of test automation
  
      File.AppendAllText(@"C:\log.txt", "Message inner");
  }
  </code></pre>
  
  <p><strong>Problem is:</strong> Some times, after method <code>executeScript()</code> reaches its final line in its thread - method <code>.Join()</code> in the main thread continues to wait for timeout. That is - text <code>"Message inner"</code> is present in the <code>"C:\log.txt"</code> file, but text <code>"Message outer"</code> is missing.</p>
  
  <p><strong>NB:</strong> Behavior described above reproduces intermittently for cases when new threads with <em>STA</em> apartment state are spawned in the beginning of <code>executeScript()</code> method. New threads perform monitoring of UI controls with <a href="http://www.ranorex.com" rel="nofollow">Ranorex</a> tools - which perform behind the scene <em>Win32 API</em> calls that I am unfamiliar with. All of new threads' references are passed to the main thread and suppose to live after thread of <code>executeScript()</code> method exists.</p>
  
  <p>Method <code>executeScript</code> makes calls with reflection according to automated script - and can potentially do any calls which can be implemented with .NET on a system.</p>
  
  <p><strong>My question is:</strong> Is it possible that invocation of new threads blocks execution of <code>executeScript()</code> method in separate thread - even after method reaches its last line? Can it be that <em>STA</em> apartment state of the thread and some Win32 calls that cause message pumping are the reason of hanging <code>.Join()</code> method for thread after thread's function passes all lines?</p>
  
  <p><em>Note:</em> Hang of <code>.Join()</code> method happens very rarely and was reproduced only on lab machines. I did not manage to reproduce behavior on local machine - even after automatic executing hundreds of times overnight.</p>
  
  <p><strong>Workaround found</strong>: So far I have ended up with following work around - resorted to usage of <code>ManualResetEventSlim</code> to wait for completion of the thread as below:</p>
  
  <pre><code>private ManualResetEventSlim executionControl = new ManualResetEventSlim();
  private void runScriptSeparateThread(TestScript script)
  {
      this.executionControl.Reset();
  
      Thread runScriptThread = new Thread(() =&gt; executeScript(script));
      runScriptThread.SetApartmentState(ApartmentState.STA);
      runScriptThread.Start();
  
      if (this.executionControl.Wait(timeout))
      {
          runScriptThread.Abort();
          File.AppendAllText(@"C:\log.txt", "Message outer");
      }
      else
      {
          File.AppendAllText(@"C:\log.txt", "Error: timeout ");
      }
  }
  
  void executeScript(TestScript script)
  {
      // execute test automation
  
      File.AppendAllText(@"C:\log.txt", "Message inner");
      this.executionControl.Set();
  }
  </code></pre>
  
  <p>Posted <a href="http://social.msdn.microsoft.com/Forums/vstudio/en-US/063d256d-1bcd-4168-8a19-ec99175de431/threadjoin-hangs-seldomly-after-thread-passes-its-last-line?forum=netfxbcl#063d256d-1bcd-4168-8a19-ec99175de431" rel="nofollow">the same question</a> on MSDN forum.</p>