ThreadState Enumeration (System.Threading)

Switch View :
ScriptFree
.NET Framework Class Library
ThreadState Enumeration

Updated: March 2011

Specifies the execution states of a Thread.

This enumeration has a FlagsAttribute attribute that allows a bitwise combination of its member values.

Namespace:  System.Threading
Assembly:  mscorlib (in mscorlib.dll)
Syntax

Visual Basic
<SerializableAttribute> _
<ComVisibleAttribute(True)> _
<FlagsAttribute> _
Public Enumeration ThreadState
C#
[SerializableAttribute]
[ComVisibleAttribute(true)]
[FlagsAttribute]
public enum ThreadState
Visual C++
[SerializableAttribute]
[ComVisibleAttribute(true)]
[FlagsAttribute]
public enum class ThreadState
F#
[<SerializableAttribute>]
[<ComVisibleAttribute(true)>]
[<FlagsAttribute>]
type ThreadState
Members

Member name Description
Running The thread has been started, it is not blocked, and there is no pending ThreadAbortException.
StopRequested The thread is being requested to stop. This is for internal use only.
SuspendRequested The thread is being requested to suspend.
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.
Unstarted The Thread.Start method has not been invoked on the thread.
Stopped The thread has stopped.
WaitSleepJoin The thread is blocked. This could be the result of calling Thread.Sleep or Thread.Join, of requesting a lock — for example, by calling Monitor.Enter or Monitor.Wait — or of waiting on a thread synchronization object such as ManualResetEvent.
Suspended The thread has been suspended.
AbortRequested The Thread.Abort method has been invoked on the thread, but the thread has not yet received the pending System.Threading.ThreadAbortException that will attempt to terminate it.
Aborted The thread state includes AbortRequested and the thread is now dead, but its state has not yet changed to Stopped.
Remarks

The ThreadState enumeration is of interest only in a few debugging scenarios. Your code should never use the thread state to synchronize the activities of threads.

ThreadState defines a set of all possible execution states for threads. Once a thread is created, it is in at least one of the states until it terminates. Threads created within the common language runtime are initially in the Unstarted state, while external threads that come into the runtime are already in the Running state. An Unstarted thread is transitioned into the Running state by calling Start. Not all combinations of ThreadState values are valid; for example, a thread cannot be in both the Aborted and Unstarted states.

Note Note

There are two thread state enumerations, System.Threading.ThreadState and System.Diagnostics.ThreadState.

The following table shows the actions that cause a change of state.

Action

ThreadState

A thread is created within the common language runtime.

Unstarted

Another thread calls the Thread.Start method on the new thread, and the call returns.

Note Note
The Start method does not return until the new thread has started running. There is no way to know at what point the new thread will start running during the call to Start.

Running

The thread calls Sleep.

WaitSleepJoin

The thread calls Wait on another object.

WaitSleepJoin

The thread calls Join on another thread.

WaitSleepJoin

Another thread calls Interrupt.

Running

Another thread calls Suspend.

SuspendRequested

The thread responds to a Suspend request.

Suspended

Another thread calls Resume.

Running

Another thread calls Abort.

AbortRequested

The thread responds to a Abort request.

Stopped

The 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 Wait, and another thread calls 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 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 whether a thread is running by using C# code such as (myThread.ThreadState & (ThreadState.Stopped | ThreadState.Unstarted)) == 0 or Visual Basic code such as (myThread.ThreadState And (ThreadState.Stopped Or ThreadState.Unstarted)) = 0.

Version Information

.NET Framework

Supported in: 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1
Platforms

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
See Also

Reference

Other Resources

Change History

Date

History

Reason

March 2011

Clarified the relationship between the Thread.Start method and the Running state, and emphasized that the ThreadState enumeration should not be used to synchronize the activities of threads.

Content bug fix.