Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

Thread.ThreadState Property

Gets a value containing the states of the current thread.

Namespace: System.Threading
Assembly: mscorlib (in mscorlib.dll)

'Declaration
Public ReadOnly Property ThreadState As ThreadState
'Usage
Dim instance As Thread
Dim value As ThreadState

value = instance.ThreadState

/** @property */
public ThreadState get_ThreadState ()

public function get ThreadState () : ThreadState

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.

NoteImportant:

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.

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

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

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0, 1.1, 1.0
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.