Auf Englisch lesen Bearbeiten

Freigeben über


Thread.ThreadState Property

Definition

Important

Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.

Gets a value containing the states of the current thread.

C#
public System.Threading.ThreadState ThreadState { get; }

Property Value

One of the ThreadState values indicating the state of the current thread. The initial value is Unstarted.

Examples

The following code example demonstrates accessing the ThreadState of a thread.

C#
using System;
using System.Threading;

class Example
{
    static void Main()
    {
        Thread newThread = 
            new Thread(new ThreadStart(ThreadMethod));

        Console.WriteLine("ThreadState: {0}", newThread.ThreadState);
        newThread.Start();

        // Wait for newThread to start and go to sleep.
        Thread.Sleep(300);
        Console.WriteLine("ThreadState: {0}", newThread.ThreadState);
        
        // Wait for newThread to restart.
        Thread.Sleep(1000);
        Console.WriteLine("ThreadState: {0}", newThread.ThreadState);
    }

    static void ThreadMethod()
    {
        Thread.Sleep(1000);
    }
}
// The example displays the following output:
//       ThreadState: Unstarted
//       ThreadState: WaitSleepJoin
//       ThreadState: Stopped

Remarks

The ThreadState property provides more specific information than the IsAlive property.

Wichtig

Thread state is only of interest in debugging scenarios. Your code should never use thread state to synchronize the activities of threads.

Applies to

Produkt Versionen
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1