.NET Framework Class Library
ThreadStateException Class

The exception that is thrown when a Thread is in an invalid ThreadState for the method call.

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

Visual Basic (Declaration)
<SerializableAttribute> _
<ComVisibleAttribute(True)> _
Public Class ThreadStateException _
    Inherits SystemException
Visual Basic (Usage)
Dim instance As ThreadStateException
C#
[SerializableAttribute]
[ComVisibleAttribute(true)]
public class ThreadStateException : SystemException
Visual C++
[SerializableAttribute]
[ComVisibleAttribute(true)]
public ref class ThreadStateException : public SystemException
JScript
public class ThreadStateException extends SystemException
Remarks

Once a thread is created, it is in at least one of the ThreadState states until it terminates. ThreadStateException is thrown by methods that cannot perform the requested operation due to the current state of a thread. For example, trying to restart an aborted thread by calling Start on a thread that has terminated throws a ThreadStateException.

ThreadStateException uses the HRESULT COR_E_THREADSTATE, which has the value 0x80131520.

For a list of initial property values for an instance of ThreadStateException, see the ThreadStateException constructors.

Examples

The following example demonstrates an error that causes the system to throw a ThreadStateException.

Visual Basic
Imports System
Imports System.Threading

Public Class ThreadWork

   Public Shared Sub DoWork()
      Console.WriteLine("Working thread...")
   End Sub 'DoWork
End Class 'ThreadWork

Class ThreadStateTest

   Public Shared Sub Main()
      Dim myThreadDelegate As New ThreadStart(AddressOf ThreadWork.DoWork)
      Dim myThread As New Thread(myThreadDelegate)
      myThread.Start()
      Thread.Sleep(0)
      Console.WriteLine("In main. Attempting to restart myThread.")
      Try
         myThread.Start()
      Catch e As ThreadStateException
         Console.WriteLine("Caught: {0}", e.Message)
      End Try
   End Sub 'Main
End Class 'ThreadStateTest
C#
using System;
using System.Threading;

public class ThreadWork 
{
public static void DoWork()
   {
   Console.WriteLine("Working thread..."); 
   }
}

class ThreadStateTest
{
public static void Main()
   {
   ThreadStart myThreadDelegate = new ThreadStart(ThreadWork.DoWork);
   Thread myThread = new Thread(myThreadDelegate);
   myThread.Start();
   Thread.Sleep(0);
   Console.WriteLine("In main. Attempting to restart myThread.");
   try 
      {
      myThread.Start();
      }
      catch(ThreadStateException e)
      {
      Console.WriteLine("Caught: {0}", e.Message);
      }
   }
}
Visual C++
using namespace System;
using namespace System::Threading;
ref class ThreadWork
{
public:
   static void DoWork()
   {
      Console::WriteLine( "Working thread..." );
   }

};

int main()
{
   ThreadStart^ myThreadDelegate = gcnew ThreadStart( ThreadWork::DoWork );
   Thread^ myThread = gcnew Thread( myThreadDelegate );
   myThread->Start();
   Thread::Sleep( 0 );
   Console::WriteLine( "In main. Attempting to restart myThread." );
   try
   {
      myThread->Start();
   }
   catch ( ThreadStateException^ e ) 
   {
      Console::WriteLine( "Caught: {0}", e->Message );
   }

}

This code produces the following output:

 In main. Attempting to restart myThread.
     Working thread...
     Caught: Thread is running or terminated. Cannot restart.
Inheritance Hierarchy

System..::.Object
  System..::.Exception
    System..::.SystemException
      System.Threading..::.ThreadStateException
Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Platforms

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360, Zune

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Version Information

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 3.5, 2.0, 1.0

XNA Framework

Supported in: 3.0, 2.0, 1.0
See Also

Reference

Other Resources

Tags :


Page view tracker