Thread.ThreadState Eigenschaft
Definition
Wichtig
Einige Informationen beziehen sich auf Vorabversionen, die vor dem Release ggf. grundlegend überarbeitet werden. Microsoft übernimmt hinsichtlich der hier bereitgestellten Informationen keine Gewährleistungen, seien sie ausdrücklich oder konkludent.
Ruft einen Wert ab, der die Zustände des aktuellen Threads enthält.
public:
property System::Threading::ThreadState ThreadState { System::Threading::ThreadState get(); };
public System.Threading.ThreadState ThreadState { get; }
member this.ThreadState : System.Threading.ThreadState
Public ReadOnly Property ThreadState As ThreadState
Eigenschaftswert
Einer der ThreadState Werte, der den Status des aktuellen Threads angibt. Der Anfangswert ist Unstarted.
Beispiele
Im folgenden Codebeispiel wird der Zugriff auf einen ThreadState Thread veranschaulicht.
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
open System.Threading
let threadMethod () =
Thread.Sleep 1000
let newThread = Thread threadMethod
printfn $"ThreadState: {newThread.ThreadState}"
newThread.Start()
// Wait for newThread to start and go to sleep.
Thread.Sleep 300
printfn $"ThreadState: {newThread.ThreadState}"
// Wait for newThread to restart.
Thread.Sleep 1000
printfn $"ThreadState: {newThread.ThreadState}"
// The example displays the following output:
// ThreadState: Unstarted
// ThreadState: WaitSleepJoin
// ThreadState: Stopped
Imports System.Threading
Public Module Example
Public Sub Main()
Dim newThread As Thread = New Thread(AddressOf 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)
End Sub
Sub ThreadMethod()
Thread.Sleep(1000)
End Sub
End Module
' The example displays the following output:
' ThreadState: Unstarted
' ThreadState: WaitSleepJoin
' ThreadState: Stopped
Hinweise
Die ThreadState Eigenschaft enthält spezifischere Informationen als die IsAlive Eigenschaft.
Von Bedeutung
Threadstatus ist nur für Debuggingszenarien von Interesse. Ihr Code sollte niemals den Threadzustand verwenden, um die Aktivitäten von Threads zu synchronisieren.