Thread.ThreadState 属性
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
获取一个值,该值包含当前线程的状态。
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
属性值
指示 ThreadState 当前线程状态的值之一。 初始值为 Unstarted。
示例
下面的代码示例演示如何访问 ThreadState 线程。
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
注解
该 ThreadState 属性提供比属性更具体的信息 IsAlive 。
重要
线程状态仅对调试方案感兴趣。 代码不应使用线程状态来同步线程的活动。