Thread.ThreadState 属性

获取一个值,该值包含当前线程的状态。

**命名空间:**System.Threading
**程序集:**mscorlib(在 mscorlib.dll 中)

语法

声明
Public ReadOnly Property ThreadState As ThreadState
用法
Dim instance As Thread
Dim value As ThreadState

value = instance.ThreadState
public ThreadState ThreadState { get; }
public:
property ThreadState ThreadState {
    ThreadState get ();
}
/** @property */
public ThreadState get_ThreadState ()
public function get ThreadState () : ThreadState

属性值

ThreadState 值之一,它指示当前线程的状态。初始值为 Unstarted

备注

ThreadState 属性比 IsAlive 属性能提供更多的特定信息。

Note重要事项:

只在几个调试方案中涉及线程状态。您的代码在任何情况下都不应使用线程状态来同步线程的活动。

示例

下面的代码示例演示如何访问线程的 ThreadState

Imports Microsoft.VisualBasic
Imports System
Imports System.Threading

Public Class ApartmentTest

    <MTAThread> _
    Shared Sub Main()
    
        Dim newThread As Thread = New Thread(AddressOf ThreadMethod)
        newThread.SetApartmentState(ApartmentState.MTA)

        ' The following line is ignored since 
        ' ApartmentState can only be set once.
        newThread.SetApartmentState(ApartmentState.STA)

        Console.WriteLine("ThreadState: {0}, ApartmentState: {1}", _
            newThread.ThreadState, newThread.GetApartmentState())

        newThread.Start()

        ' Wait for newThread to start and go to sleep.
        Thread.Sleep(300)
        Try
            ' This causes an exception since newThread is sleeping.
            newThread.SetApartmentState(ApartmentState.STA)
        Catch stateException As ThreadStateException
            Console.WriteLine(vbCrLf & "{0} caught:" & vbCrLf & _
                "Thread is not In the Unstarted or Running state.", _
                stateException.GetType().Name)
            Console.WriteLine("ThreadState: {0}, ApartmentState: " & _
                "{1}", newThread.ThreadState, newThread.GetApartmentState())
        End Try

    End Sub

    Shared Sub ThreadMethod()
        Thread.Sleep(1000)
    End Sub

End Class
using System;
using System.Threading;

class ApartmentTest
{
    static void Main()
    {
        Thread newThread = 
            new Thread(new ThreadStart(ThreadMethod));
        newThread.SetApartmentState(ApartmentState.MTA);

        // The following line is ignored since 
        // ApartmentState can only be set once.
        newThread.SetApartmentState(ApartmentState.STA);

        Console.WriteLine("ThreadState: {0}, ApartmentState: {1}", 
            newThread.ThreadState, newThread.ApartmentState);

        newThread.Start();

        // Wait for newThread to start and go to sleep.
        Thread.Sleep(300);
        try
        {
            // This causes an exception since newThread is sleeping.
            newThread.SetApartmentState(ApartmentState.STA);
        }
        catch(ThreadStateException stateException)
        {
            Console.WriteLine("\n{0} caught:\n" +
                "Thread is not in the Unstarted or Running state.", 
                stateException.GetType().Name);
            Console.WriteLine("ThreadState: {0}, ApartmentState: {1}",
                newThread.ThreadState, newThread.GetApartmentState());
        }
    }

    static void ThreadMethod()
    {
        Thread.Sleep(1000);
    }
}
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 ) );
   newThread->SetApartmentState(ApartmentState::MTA);
   
   // The following line is ignored since 
   // ApartmentState can only be set once.
   newThread->SetApartmentState(ApartmentState::STA);
   Console::WriteLine( "ThreadState: {0}, ApartmentState: {1}", newThread->ThreadState.ToString(), newThread->GetApartmentState().ToString() );
   newThread->Start();
   
   // Wait for newThread to start and go to sleep.
   Thread::Sleep( 300 );
   try
   {
      
      // This causes an exception since newThread is sleeping.
      newThread->SetApartmentState(ApartmentState::STA);
   }
   catch ( ThreadStateException^ stateException ) 
   {
      Console::WriteLine( "\n{0} caught:\n"
      "Thread is not in the Unstarted or Running state.", stateException->GetType()->Name );
      Console::WriteLine( "ThreadState: {0}, ApartmentState: {1}", newThread->ThreadState.ToString(), newThread->GetApartmentState().ToString() );
   }

}
import System.*;
import System.Threading.*;
import System.Threading.Thread;    

class ApartmentTest
{
    public static void main(String[] args)
    {
        Thread newThread = new Thread(new ThreadStart(ThreadMethod));

        newThread.set_ApartmentState(ApartmentState.MTA);

        // The following line is ignored since 
        // ApartmentState can only be set once.
        newThread.set_ApartmentState(ApartmentState.STA);
        Console.WriteLine("ThreadState: {0}, ApartmentState: {1}",
                        newThread.get_ThreadState(),
                        newThread.get_ApartmentState());
        newThread.Start();

        // Wait for newThread to start and go to sleep.
        Thread.Sleep(300);
        try {
            // This causes an exception since newThread is sleeping.
            newThread.set_ApartmentState(ApartmentState.STA);
        }
        catch (ThreadStateException stateException) {
            Console.WriteLine("\n{0} caught:\n" + 
                "Thread is not in the Unstarted or Running state.", 
                stateException.GetType().get_Name());
            Console.WriteLine("ThreadState: {0}, ApartmentState: {1}",
                newThread.get_ThreadState(),newThread.get_ApartmentState());
        }
    } //main

    static void ThreadMethod()
    {
        Thread.Sleep(1000);
    } //ThreadMethod
} //ApartmentTest

平台

Windows 98、Windows 2000 SP4、Windows CE、Windows Millennium Edition、Windows Mobile for Pocket PC、Windows Mobile for Smartphone、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1、1.0

请参见

参考

Thread 类
Thread 成员
System.Threading 命名空间

其他资源

托管线程状态