System.Threading.ThreadStateException Class

public class ThreadStateException : SystemException

Base Types

Object
  Exception
    SystemException
      ThreadStateException

Assembly

mscorlib

Library

BCL

Summary

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.

Description

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.

Example

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.

See Also

System.Threading Namespace

Members

ThreadStateException Constructors

ThreadStateException() Constructor
ThreadStateException(System.String) Constructor
ThreadStateException(System.String, System.Exception) Constructor


ThreadStateException() Constructor

public ThreadStateException();

Summary

Constructs and initializes a new instance of the ThreadStateException class.

Description

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 .

See Also

System.Threading.ThreadStateException Class, System.Threading Namespace

ThreadStateException(System.String) Constructor

public ThreadStateException(string message);

Summary

Constructs and initializes a new instance of the ThreadStateException class.

Parameters

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.

Description

This constructor initializes the System.Threading.ThreadStateException.Message property of the new instance using message. If message is null , 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 .

See Also

System.Threading.ThreadStateException Class, System.Threading Namespace

ThreadStateException(System.String, System.Exception) Constructor

public ThreadStateException(string message, Exception innerException);

Summary

Constructs and initializes a new instance of the ThreadStateException class.

Parameters

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.

Description

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 is null , 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.]

See Also

System.Threading.ThreadStateException Class, System.Threading Namespace