[Flags] |
The following table shows the actions that cause a change of state.
Action | ThreadState |
---|---|
A thread is created within the common language runtime. | Unstarted |
A thread calls Thread.Start | Running |
The thread starts running. | Running |
The thread calls Thread.Sleep | WaitSleepJoin |
The thread calls Monitor.Wait on another object. | WaitSleepJoin |
The thread calls Thread.Join on another thread. | WaitSleepJoin |
Another thread calls Thread.Interrupt | Running |
Another thread calls Thread.Suspend | SuspendRequested |
The thread responds to a Thread.Suspend request. | Suspended |
Another thread calls Thread.Resume | Running |
Another thread calls Thread.Abort | AbortRequested |
The thread responds to a Thread.Abort request. | Stopped |
A thread is terminated. | Stopped |
In addition to the states noted above, there is also the Background state, which indicates whether the thread is running in the background or foreground.
A thread can be in more than one state at a given time. For example, if a thread is blocked on a call to Monitor.Wait, and another thread calls Thread.Abort on the blocked thread, the blocked thread will be in both the WaitSleepJoin and the AbortRequested states at the same time. In this case, as soon as the thread returns from the call to Monitor.Wait or is interrupted, it will receive the ThreadAbortException to begin aborting.
The Thread.ThreadState property of a thread provides the current state of a thread. Applications must use a bitmask to determine whether a thread is running. Since the value for Running is zero (0), test if a thread is running by using code such as
((threadState
& (Stopped | Unstarted)) == 0
.
Aborted | The thread is in the Stopped state. |
AbortRequested | The Thread.Abort method has been invoked on the thread, but the thread has not yet received the pending ThreadAbortException that will attempt to terminate it. |
Background | The thread is being executed as a background thread, as opposed to a foreground thread. This state is controlled by setting the Thread.IsBackground property. |
Running | The thread has been started, it is not blocked, and there is no pending ThreadAbortException. |
Stopped | The thread has stopped. |
StopRequested | The thread is being requested to stop. This is for internal use only. |
Suspended | The thread has been suspended. |
SuspendRequested | The thread is being requested to suspend. |
Unstarted | The Thread.Start method has not been invoked on the thread. |
WaitSleepJoin | The thread is blocked as a result of a call to Monitor.Wait, Thread.Sleep, or Thread.Join. |
Hierarchy: