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.
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
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.
using namespace System;
using namespace System::Threading;
// ref class ApartmentTest
// {
// public:
static void ThreadMethod()
{
Thread::Sleep( 1000 );
// }
};
int main()
{
// Thread^ newThread = gcnew Thread( gcnew ThreadStart( &ApartmentTest::ThreadMethod ) );
Thread^ newThread = gcnew Thread( gcnew 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);
}
// The example displays the following output:
// ThreadState: Unstarted
// ThreadState: WaitSleepJoin
// ThreadState: Stopped
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
Remarks
The ThreadState property provides more specific information than the IsAlive property.
Important
Thread state is only of interest in debugging scenarios. Your code should never use thread state to synchronize the activities of threads.
Applies to
反馈
https://aka.ms/ContentUserFeedback。
即将发布:在整个 2024 年,我们将逐步淘汰作为内容反馈机制的“GitHub 问题”,并将其取代为新的反馈系统。 有关详细信息,请参阅:提交和查看相关反馈