This documentation is archived and is not being maintained.
Thread::ThreadState Property
Visual Studio 2010
Gets a value containing the states of the current thread.
Assembly: mscorlib (in mscorlib.dll)
Property Value
Type: System.Threading::ThreadStateOne of the ThreadState values indicating the state of the current thread. The initial value is Unstarted.
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. |
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 ) ); 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() ); } }
Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Show:
Important