Mono Class Library: System.Threading Namespace

System.Threading.ThreadStateException Class

Represents errors that occur when a method is invoked on a System.Threading.Thread and the thread is in a Thread.ThreadState that is invalid for the method. [Edit]

See Also: ThreadStateException Members

System.Object
     System.Exception
          System.SystemException
               System.Threading.ThreadStateException

[System.Runtime.InteropServices.ComVisible(true)]
public class ThreadStateException : SystemException

Thread Safety

All public static members of this type are safe for multithreaded operations. No instance members are guaranteed to be thread safe.

Remarks

Once a thread is created, it is in one or more states, as defined by System.Threading.ThreadState, until it terminates. System.Threading.ThreadStateException is thrown by methods that cannot perform the requested operation due to the current state of a thread. For example, calling Thread.Start on a thread that has terminated results in a System.Threading.ThreadStateException exception. [Edit]

Example

The following example demonstrates an error that causes a System.Threading.ThreadStateException exception to be thrown.

C# Example
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);
 }
 
 }
}
 

The output is

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

Requirements

Namespace: System.Threading
Assembly: mscorlib (in mscorlib.dll)
Assembly Versions: 1.0.5000.0, 2.0.0.0