Thread.ThreadState Propriedade
Definição
Obtém um valor que contém os estados do thread atual.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
Valor da propriedade
Um dos valores de ThreadState que indica o estado do thread atual.One of the ThreadState values indicating the state of the current thread. O valor inicial é Unstarted.The initial value is Unstarted.
Exemplos
O exemplo de código a seguir demonstra o acesso ao ThreadState
de um thread.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
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
Comentários
A ThreadState propriedade fornece informações mais específicas do que a IsAlive propriedade.The ThreadState property provides more specific information than the IsAlive property.
Importante
O estado do thread é apenas de interesse em cenários de depuração.Thread state is only of interest in debugging scenarios. O código nunca deve usar o estado do thread para sincronizar as atividades de threads.Your code should never use thread state to synchronize the activities of threads.