Managed Thread States
Updated: May 2011
The property Thread.ThreadState provides a bit mask that indicates the thread's current state. A thread is always in at least one of the possible states in the ThreadState enumeration, and can be in multiple states at the same time.
Important |
---|
Thread state is only of interest in a few debugging scenarios. Your code should never use thread state to synchronize the activities of threads. |
When you create a managed thread, it is in the ThreadState.Unstarted state. The thread remains in the Unstarted state until you call the Thread.Start method, which puts the thread into the Running state so that the operating system can schedule it for execution.
Unmanaged threads that enter the managed environment are already started. Once a thread is started, a number of actions can cause it to change states. The following table lists the actions that cause a change of state, along with the corresponding new state.
Action |
Resulting new state |
---|---|
The constructor for the Thread class is called. |
|
Another thread calls the Thread.Start method on the new thread, and the call returns. |
|
The thread calls Thread.Sleep. |
|
The thread calls Monitor.Wait on another object. |
|
The thread calls Thread.Join on another thread. |
|
Another thread calls Thread.Suspend. |
|
The thread responds to a Thread.Suspend request. |
|
Another thread calls Thread.Resume. |
|
Another thread calls Thread.Abort. |
|
The thread responds to an Thread.Abort. |
Because the Running state has a value of 0, it is not possible to perform a bit test to discover this state. Instead, the following test (in pseudo-code) can be used:
if ((state & (Unstarted | Stopped)) == 0) // implies Running
Threads are often in more than one state at any given time. For example, if a thread is blocked on a Monitor.Wait call and another thread calls Abort on that same thread, the thread will be in both the WaitSleepJoin and the AbortRequested states at the same time. In that case, as soon as the thread returns from the call to Wait or is interrupted, it will receive the ThreadAbortException.
Once a thread leaves the Unstarted state as the result of a call to Start, it can never return to the Unstarted state. A thread can never leave the Stopped state.
See Also
Reference
Other Resources
Change History
Date |
History |
Reason |
---|---|---|
May 2011 |
Further clarification of the relationship between the Thread.Start method and the Running state. |
Customer feedback. |
March 2011 |
Clarified the relationship between the Thread.Start method and the Running state. |
Content bug fix. |