Thread.ThreadState Property
.NET Framework 3.0
Gets a value containing the states of the current thread.
Namespace: System.Threading
Assembly: mscorlib (in mscorlib.dll)
Assembly: mscorlib (in mscorlib.dll)
/** @property */ public ThreadState get_ThreadState ()
public function get ThreadState () : ThreadState
Not applicable.
Property Value
One 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 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); } }
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 Server 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.Community Additions
ADD
Show:
Important: