ThreadInterruptedException Třída

Definice

Výjimka, která se vyvolá, když Thread je přerušena, když je ve stavu čekání.

public ref class ThreadInterruptedException : SystemException
public class ThreadInterruptedException : SystemException
[System.Serializable]
public class ThreadInterruptedException : SystemException
[System.Serializable]
[System.Runtime.InteropServices.ComVisible(true)]
public class ThreadInterruptedException : SystemException
type ThreadInterruptedException = class
    inherit SystemException
[<System.Serializable>]
type ThreadInterruptedException = class
    inherit SystemException
[<System.Serializable>]
[<System.Runtime.InteropServices.ComVisible(true)>]
type ThreadInterruptedException = class
    inherit SystemException
Public Class ThreadInterruptedException
Inherits SystemException
Dědičnost
ThreadInterruptedException
Atributy

Příklady

Následující příklad kódu ukazuje chování spuštěného vlákna při přerušení a následně se zablokuje.

using namespace System;
using namespace System::Security::Permissions;
using namespace System::Threading;

ref class StayAwake
{
private:
   bool sleepSwitch;

public:

   property bool SleepSwitch 
   {
      void set( bool value )
      {
         sleepSwitch = value;
      }

   }
   StayAwake()
   {
      sleepSwitch = false;
   }

   void ThreadMethod()
   {
      Console::WriteLine( "newThread is executing ThreadMethod." );
      while (  !sleepSwitch )
      {
         
         // Use SpinWait instead of Sleep to demonstrate the 
         // effect of calling Interrupt on a running thread.
         Thread::SpinWait( 10000000 );
      }

      try
      {
         Console::WriteLine( "newThread going to sleep." );
         
         // When newThread goes to sleep, it is immediately 
         // woken up by a ThreadInterruptedException.
         Thread::Sleep( Timeout::Infinite );
      }
      catch ( ThreadInterruptedException^ /*e*/ ) 
      {
         Console::WriteLine( "newThread cannot go to sleep - "
         "interrupted by main thread." );
      }

   }

};

int main()
{
   StayAwake^ stayAwake = gcnew StayAwake;
   Thread^ newThread = gcnew Thread( gcnew ThreadStart( stayAwake, &StayAwake::ThreadMethod ) );
   newThread->Start();
   
   // The following line causes an exception to be thrown 
   // in ThreadMethod if newThread is currently blocked
   // or becomes blocked in the future.
   newThread->Interrupt();
   Console::WriteLine( "Main thread calls Interrupt on newThread." );
   
   // Then tell newThread to go to sleep.
   stayAwake->SleepSwitch = true;
   
   // Wait for newThread to end.
   newThread->Join();
}
using System;
using System.Security.Permissions;
using System.Threading;

class ThreadInterrupt
{
    static void Main()
    {
        StayAwake stayAwake = new StayAwake();
        Thread newThread = 
            new Thread(new ThreadStart(stayAwake.ThreadMethod));
        newThread.Start();

        // The following line causes an exception to be thrown 
        // in ThreadMethod if newThread is currently blocked
        // or becomes blocked in the future.
        newThread.Interrupt();
        Console.WriteLine("Main thread calls Interrupt on newThread.");

        // Tell newThread to go to sleep.
        stayAwake.SleepSwitch = true;

        // Wait for newThread to end.
        newThread.Join();
    }
}

class StayAwake
{
    bool sleepSwitch = false;

    public bool SleepSwitch
    {
        set{ sleepSwitch = value; }
    }

    public StayAwake(){}

    public void ThreadMethod()
    {
        Console.WriteLine("newThread is executing ThreadMethod.");
        while(!sleepSwitch)
        {
            // Use SpinWait instead of Sleep to demonstrate the 
            // effect of calling Interrupt on a running thread.
            Thread.SpinWait(10000000);
        }
        try
        {
            Console.WriteLine("newThread going to sleep.");

            // When newThread goes to sleep, it is immediately 
            // woken up by a ThreadInterruptedException.
            Thread.Sleep(Timeout.Infinite);
        }
        catch(ThreadInterruptedException e)
        {
            Console.WriteLine("newThread cannot go to sleep - " +
                "interrupted by main thread.");
        }
    }
}
Option Explicit
Option Strict

Imports System.Security.Permissions
Imports System.Threading

Public Class ThreadInterrupt

    <MTAThread> _
    Shared Sub Main()
        Dim stayAwake As New StayAwake()
        Dim newThread As New Thread(AddressOf stayAwake.ThreadMethod)
        newThread.Start()

        ' The following line causes an exception to be thrown 
        ' in ThreadMethod if newThread is currently blocked
        ' or becomes blocked in the future.
        newThread.Interrupt()
        Console.WriteLine("Main thread calls Interrupt on newThread.")

        ' Tell newThread to go to sleep.
        stayAwake.SleepSwitch = True

        ' Wait for newThread to end.
        newThread.Join()
    End Sub

End Class

Public Class StayAwake

    Dim sleepSwitchValue As Boolean = False

    WriteOnly Property SleepSwitch As Boolean
        Set
            sleepSwitchValue = Value
        End Set
    End Property 

    Sub New()
    End Sub

    Sub ThreadMethod()
        Console.WriteLine("newThread is executing ThreadMethod.")
        While Not sleepSwitchValue

            ' Use SpinWait instead of Sleep to demonstrate the 
            ' effect of calling Interrupt on a running thread.
            Thread.SpinWait(10000000)
        End While
        Try
            Console.WriteLine("newThread going to sleep.")

            ' When newThread goes to sleep, it is immediately 
            ' woken up by a ThreadInterruptedException.
            Thread.Sleep(Timeout.Infinite)
        Catch ex As ThreadInterruptedException
            Console.WriteLine("newThread cannot go to " & _
                "sleep - interrupted by main thread.")
        End Try
    End Sub

End Class

Poznámky

Po vytvoření vlákna se nachází v jednom nebo více ThreadState stavech, dokud ho neničíte. Volání Interrupt , když je vlákno ve WaitSleepJoin stavu, způsobí ThreadInterruptedException vyvolání v cílovém vlákně. Pokud vlákno není ve WaitSleepJoin stavu, výjimka se nevyvolá, dokud vlákno tento stav nezadá. Pokud vlákno nikdy nezablokuje, mohlo by se dokončit bez přerušení.

ThreadInterruptedException používá COR_E_THREADINTERRUPTED HRESULT, který má hodnotu 0x80131519.

Seznam počátečních hodnot vlastností pro instanci ThreadInterruptedExceptionnaleznete v ThreadInterruptedException konstruktorech.

Konstruktory

ThreadInterruptedException()

Inicializuje novou instanci ThreadInterruptedException třídy s výchozími vlastnostmi.

ThreadInterruptedException(SerializationInfo, StreamingContext)

Inicializuje novou instanci třídy ThreadInterruptedException se serializovanými daty.

ThreadInterruptedException(String)

Inicializuje novou instanci ThreadInterruptedException třídy se zadanou chybovou zprávou.

ThreadInterruptedException(String, Exception)

Inicializuje novou instanci ThreadInterruptedException třídy se zadanou chybovou zprávou a odkazem na vnitřní výjimku, která je příčinou této výjimky.

Vlastnosti

Data

Získá kolekci párů klíč/hodnota, které poskytují další uživatelem definované informace o výjimce.

(Zděděno od Exception)
HelpLink

Získá nebo nastaví odkaz na soubor nápovědy přidružený k této výjimce.

(Zděděno od Exception)
HResult

Získá nebo nastaví HRESULT, kódovanou číselnou hodnotu přiřazenou konkrétní výjimce.

(Zděděno od Exception)
InnerException

Exception Získá instanci, která způsobila aktuální výjimku.

(Zděděno od Exception)
Message

Získá zprávu, která popisuje aktuální výjimku.

(Zděděno od Exception)
Source

Získá nebo nastaví název aplikace nebo objektu, který způsobuje chybu.

(Zděděno od Exception)
StackTrace

Získá řetězcové znázornění okamžitých rámců v zásobníku volání.

(Zděděno od Exception)
TargetSite

Získá metodu, která vyvolá aktuální výjimku.

(Zděděno od Exception)

Metody

Equals(Object)

Určí, zda se zadaný objekt rovná aktuálnímu objektu.

(Zděděno od Object)
GetBaseException()

Při přepsání v odvozené třídě vrátí Exception hodnotu, která je hlavní příčinou jedné nebo více následných výjimek.

(Zděděno od Exception)
GetHashCode()

Slouží jako výchozí funkce hash.

(Zděděno od Object)
GetObjectData(SerializationInfo, StreamingContext)

Při přepsání v odvozené třídě nastaví s SerializationInfo informacemi o výjimce.

(Zděděno od Exception)
GetType()

Získá typ modulu runtime aktuální instance.

(Zděděno od Exception)
MemberwiseClone()

Vytvoří použádnou kopii aktuálního souboru Object.

(Zděděno od Object)
ToString()

Vytvoří a vrátí řetězcovou reprezentaci aktuální výjimky.

(Zděděno od Exception)

událost

SerializeObjectState
Zastaralé.

Nastane, když je výjimka serializována k vytvoření objektu stavu výjimky, který obsahuje serializovaná data o výjimce.

(Zděděno od Exception)

Platí pro

Viz také