Este artigo foi traduzido por máquina. Para visualizar o arquivo em inglês, marque a caixa de seleção Inglês. Você também pode exibir o texto Em inglês em uma janela pop-up, movendo o ponteiro do mouse sobre o texto.
Tradução
Inglês

ThreadStateException Classe

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

Namespace:  System.Threading
Assembly:  mscorlib (em mscorlib. dll)

[SerializableAttribute]
[ComVisibleAttribute(true)]
public class ThreadStateException : SystemException

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.

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

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);
      }
   }
}


import System.*;
import System.Threading.*;

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

class ThreadStateTest
{
    public static void main(String[] args)
    {
        ThreadStart myThreadDelegate = new ThreadStart(ThreadWork.DoWork);
        System.Threading.Thread myThread =
            new System.Threading.Thread(myThreadDelegate);
        myThread.Start();
        System.Threading.Thread.Sleep(0);
        Console.WriteLine("In main. Attempting to restart myThread.");
        try {
            myThread.Start();
        }
        catch (ThreadStateException e) {
            Console.WriteLine("Caught: {0}", e.get_Message());
        }
    } //main
} //ThreadStateTest


Esse código produz a saída a seguir:

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

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

Quaisquer membros públicos estático (compartilhados na Visual Basic) desse tipo são Thread seguro. Não há garantia de que qualquer membro de instância seja isento de segmentos.

Contribuições da comunidade

ADICIONAR
Mostrar: