ThreadStateException Class
The exception that is thrown when a Thread is in an invalid ThreadState for the method call.
For a list of all members of this type, see ThreadStateException Members.
System.Object
System.Exception
System.SystemException
System.Threading.ThreadStateException
[Visual Basic] <Serializable> Public Class ThreadStateException Inherits SystemException [C#] [Serializable] public class ThreadStateException : SystemException [C++] [Serializable] public __gc class ThreadStateException : public SystemException [JScript] public Serializable class ThreadStateException extends SystemException
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.
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.
Example
[Visual Basic, C#, C++] 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); } } } [C++] #using <mscorlib.dll> using namespace System; using namespace System::Threading; __gc class ThreadWork { public: static void DoWork() { Console::WriteLine(S"Working thread..."); } }; int main() { ThreadStart* myThreadDelegate = new ThreadStart(0, ThreadWork::DoWork); Thread* myThread = new Thread(myThreadDelegate); myThread->Start(); Thread::Sleep(0); Console::WriteLine(S"In main. Attempting to restart myThread."); try { myThread->Start(); } catch (ThreadStateException* e) { Console::WriteLine(S"Caught: {0}", e->Message); } }
[Visual Basic, C#, C++] This code produces the following output:
In main. Attempting to restart myThread.
Working thread...
Caught: Thread is running or terminated. Cannot restart. [JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button
in the upper-left corner of the page.
Requirements
Namespace: System.Threading
Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework
Assembly: Mscorlib (in Mscorlib.dll)
See Also
ThreadStateException Members | System.Threading Namespace | Thread | ThreadState | Managed and Unmanaged Threading | Exception | Handling and Throwing Exceptions