<p>From time to time I get a System.Threading.ThreadStateException when attempting to restart a thread. The code in question is as follows:</p>

<pre><code>// Make sure the thread is done stopping
while (this.mThread.ThreadState == ThreadState.Running)
{ 
    Thread.Sleep(0);
}
// Respawn a thread if the current one is stopped or doesn't exist
if (this.mThread == null || this.mThread.ThreadState == ThreadState.Stopped)
{ 
    this.mThread = new Thread(new ParameterizedThreadStart(Monitor)); }
// Start the thread
if (check)
{ 
    this.mThread.Start(60000); 
}
else
{   
    this.mThread.Start(0); 
}
</code></pre>

<p>So two questions - is this the correct way of doing things, and  it is, is there a way to prevent the error from occurring?</p>

[{'answer_id': 13207, 'author': 'Brad Wilson', 'author_id': 1554, 'author_profile': 'https://Stackoverflow.com/users/1554', 'pm_score': 1, 'selected': False, 'text': '<p>A ThreadStateException is thrown because you\'re trying to start a thread that\'s not in a startable state. The most likely situations would be that it\'s already running, or that it has fully exited.</p>\n\n<p>There are potentially a couple things that might be happening. First is, the thread might have transitioned from Running to StopRequested, which isn\'t fully stopped yet, so your logic doesn\'t create a new thread, and you\'re trying to start a thread which has just finished running or is about to finish running (neither of which is a valid state for restarting).</p>\n\n<p>The other possibility is that the thread was aborted. Threads which are aborted go to the Aborted state, not the Stopped state, and of course are also not valid for restarting.</p>\n\n<p>Really, the only kind of thread that is still alive that can be "restarted" is one that\'s suspended. You might want to use this conditional instead:</p>\n\n<p><code>if (this.mThread == null || this.mThread.ThreadState != ThreadState.Suspended)</code></p>\n'}, {'answer_id': 13214, 'author': 'John Richardson', 'author_id': 887, 'author_profile': 'https://Stackoverflow.com/users/887', 'pm_score': 3, 'selected': False, 'text': "<p>It's possible for a thread to be in more than one state at once therefore the ThreadState property is actually a bitmap of possible states. So testing for equality with just one state will not give you the right result. You would need to do something like:</p>\n\n<pre><code>if((mThread.ThreadState &amp; ThreadState.Running) != 0)\n</code></pre>\n\n<p>However, checking thread state is the wrong to do anything. I'm not entirely clear what you're trying to achieve but I will guess that you're waiting for a thread to terminate before restarting it. In that case you should do:</p>\n\n<pre><code>mThread.Join();\nmThread = new Thread(new ParameterizedThreadStart(Monitor));\nif(check)\n    mThread.Start(60000);\nelse\n    mThread.Start(0);\n</code></pre>\n\n<p>Although if you describe the problem you're trying to solve in more detail I'm almost certain there will be a better solution. Waiting around for a thread to end just to restart it again doesn't seem that efficient to me. Perhaps you just need some kind of inter-thread communication?</p>\n\n<p>John.</p>\n"}, {'answer_id': 13255, 'author': 'Lasse V. Karlsen', 'author_id': 267, 'author_profile': 'https://Stackoverflow.com/users/267', 'pm_score': 3, 'selected': True, 'text': "<p>The problem is that you have code that first checks if it should create a new thread object, and another piece of code that determines wether to start the thread object. Due to race conditions and similar things, your code might end up trying to call .Start on an existing thread object. Considering you don't post the details behind the <em>check</em> variable, it's impossible to know what might trigger this behavior.</p>\n\n<p>You should reorganize your code so that .Start is guaranteed to only be called on new objects. In short, you should put the Start method into the same if-statement as the one that creates a new thread object.</p>\n\n<p>Personally, I would try to reorganize the entire code so that I didn't need to create another thread, but wrap the code inside the thread object inside a loop so that the thread just keeps on going.</p>\n"}]
['https://Stackoverflow.com/questions/13170', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1185/']