public class ThreadStateException : SystemException
Object
Exception
SystemException
ThreadStateException
mscorlib
BCL
Represents errors that occur when a method is invoked on a Thread and the thread is in a System.Threading.Thread.ThreadState that is invalid for the method.
Once a thread is created, it is in one or more states, as defined by ThreadState, until it terminates. ThreadStateException is thrown by methods that cannot perform the requested operation due to the current state of a thread. For example, calling System.Threading.Thread.Start on a thread that has terminated results in a ThreadStateException exception.
The following example demonstrates an error that causes a ThreadStateException exception to be thrown.
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.
System.Threading Namespace
ThreadStateException Constructors
ThreadStateException() Constructor
ThreadStateException(System.String) Constructor
ThreadStateException(System.String, System.Exception) Constructor
public ThreadStateException();
Constructs and initializes a new instance of the ThreadStateException class.
This constructor initializes the System.Threading.ThreadStateException.Message property of the new instance to a system-supplied message that describes the error, such as "The requested operation cannot be performed on the thread due to its current state." This message takes into account the current system culture.The System.Threading.ThreadStateException.InnerException property of the new instance is initialized to
null
.
System.Threading.ThreadStateException Class, System.Threading Namespace
public ThreadStateException(string message);
Constructs and initializes a new instance of the ThreadStateException class.
- message
- A String that describes the error. The content of message is intended to be understood by humans. The caller of this constructor is required to ensure that this string has been localized for the current system culture.
This constructor initializes the System.Threading.ThreadStateException.Message property of the new instance using message. If message isnull
, the System.Threading.ThreadStateException.Message property is initialized to the system-supplied message provided by the constructor that takes no arguments.The System.Threading.ThreadStateException.InnerException property of the new instance is initialized to
null
.
System.Threading.ThreadStateException Class, System.Threading Namespace
public ThreadStateException(string message, Exception innerException);
Constructs and initializes a new instance of the ThreadStateException class.
- message
- A String that describes the error. The content of message is intended to be understood by humans. The caller of this constructor is required to ensure that this string has been localized for the current system culture.
- innerException
- An instance of Exception that is the cause of the current exception. If innerException is non-null, then the current Exception was raised in a catch block handling innerException.
This constructor initializes the System.Threading.ThreadStateException.Message property of the new instance using message and the System.Threading.ThreadStateException.InnerException property using innerException. If message isnull
, the System.Threading.ThreadStateException.Message property is initialized to the system-supplied message provided by the constructor that takes no arguments.[Note: For more information on inner exceptions, see System.Exception.InnerException.]
System.Threading.ThreadStateException Class, System.Threading Namespace