title: New Thread Occasionally Throws "System.Threading.ThreadStateException: Thread is running or terminated; it cannot restart."
body: <p>I have a Windows service that spawns different tasks in threads at different intervals.  Some happen once a day, some happen every couple minutes (like the one that is giving me an issue).  </p>
  
  <p>My goal was to code it so that a new thread will be created for the task, but only if the previous time it was spawned has completed (or it has never been created / first run).  There should never be more than one "checkThread" thread running at a time.</p>
  
  <p>I have other areas where I use the same code as below to check if I should spawn the new thread or not, and those all work without issue, but with one of them I get an exception very infrequently:</p>
  
  <pre><code>System.Threading.ThreadStateException: Thread is running or terminated; it cannot restart.
  </code></pre>
  
  <p>The following are defined outside of the main for{} loop and are used to track what's going on:</p>
  
  <pre><code>    const int MINUTES_CHECK = 1;
      DateTime lastCheck = DateTime.Now.AddMinutes(-MINUTES_CHECK); //force this to run immediately on first run
      Thread checkThread; //used to poll status of thread
  
      const int DIFFERENT_MINUTES_CHECK = 5; //maybe this one only runs every 5 minutes
      DateTime different_LastCheck = DateTime.Now.AddMinutes(-DIFFERENT_MINUTES_CHECK); //force this to run immediately on first run
      Thread different_checkThread; //used to poll status of thread
  </code></pre>
  
  <p>This is inside the service's main for{} loop and is how I check if the thread should be created or not.  The goal is only to start a new checkThread if it has never been started before, or of the previous one is not still running:</p>
  
  <pre><code>// Worker thread loop
  for (; ; )
  {
  
      if ((DateTime.Now - lastCheck).TotalMinutes &gt;= MINUTES_CHECK)
      {
          if (checkThread == null ||
              (checkThread.ThreadState != System.Threading.ThreadState.Running &amp;&amp;
               checkThread.ThreadState != System.Threading.ThreadState.WaitSleepJoin)
              )
          {
              checkThread = new Thread(DoCheck);
              checkThread.Start();
              Console.WriteLine("Checking for items...");
              lastCheck = DateTime.Now;
          }
  
      }
  
  
      if ((DateTime.Now - different_LastCheck).TotalMinutes &gt;= DIFFERENT_MINUTES_CHECK)
      {
          if (different_checkThread== null ||
              (different_checkThread.ThreadState != System.Threading.ThreadState.Running &amp;&amp;
               different_checkThread.ThreadState != System.Threading.ThreadState.WaitSleepJoin)
              )
          {
              different_checkThread= new Thread(DoSomethingElse);
              different_checkThread.Start();
              Console.WriteLine("Checking for something else...");
              different_LastCheck = DateTime.Now;
          }
  
      }
  
  
  //// Run this code once every N milliseconds
  var wait_for = new TimeSpan(0, 0, 0, 0, 1000);
  if (mStop.WaitOne(wait_for)) return;
  }
  </code></pre>
  
  <p>It works 99.9% of the time, and so far I have been unable to get it to occur in Visual Studio.  I have run it for a couple hours in VS with no issue, and even loaded the CPU to 99% and ran if for half an hour or so and still did not get the exception.</p>
  
  <p>I did the above because my suspicion is that it's trying to start the new thread and assign it to "checkThread" before the previous one has finished (although this does seem unlikely since there's really not all that much going on inside "DoCheck" and 1 minute should be more than enough for it to complete).</p>
  
  <p>Am I missing another possible ThreadState, or is there something else going on?</p>
  
  <p>Edit:  Exception occurs on checkThread.Start();</p>