Thread.ThreadState Propriedade

Definição

Obtém um valor que contém os estados do thread atual.

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

ThreadState

Um dos valores de ThreadState que indica o estado do thread atual. O valor inicial é Unstarted.

Exemplos

O exemplo de código a seguir demonstra o acesso ao ThreadState de um 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.

Importante

O estado do thread é apenas de interesse em cenários de depuração. O código nunca deve usar o estado do thread para sincronizar as atividades de threads.

Aplica-se a