Thread.ThreadState Propriété

Définition

Obtient une valeur contenant les états du thread actuel.

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

Valeur de propriété

ThreadState

Une des valeurs ThreadState indiquant l'état du thread actuel. La valeur initiale est Unstarted.

Exemples

L’exemple de code suivant montre comment accéder au ThreadState d’un 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

Remarques

La ThreadState propriété fournit des informations plus spécifiques que la IsAlive propriété.

Important

L’état du thread est uniquement intéressant dans les scénarios de débogage. Votre code ne doit jamais utiliser l’état des threads pour synchroniser les activités des threads.

S’applique à