ThreadInterruptedException Osztály

Definíció

Az a kivétel, amely akkor fordul elő, ha egy Thread várakozó állapotban megszakad.

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
Öröklődés
ThreadInterruptedException
Attribútumok

Példák

Az alábbi példakód egy futó szál viselkedését mutatja be, amikor az megszakad, és ezt követően le lesz tiltva.

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

Megjegyzések

A szál létrehozása után egy vagy több ThreadState állapotban van, amíg el nem pusztítják. Ha egy szál állapotban Interrupt van, a hívás WaitSleepJoin a ThreadInterruptedException célszálba kerül. Ha a szál nincs állapotban WaitSleepJoin , a rendszer nem veti ki a kivételt addig, amíg a szál be nem lép az adott állapotba. Ha a szál soha nem blokkol, képes lehet befejezni anélkül, hogy valaha is megszakadna.

ThreadInterruptedException A HRESULT COR_E_THREADINTERRUPTED használja, amelynek értéke 0x80131519.

A ThreadInterruptedException példány kezdeti tulajdonságainak listájáért tekintse meg a ThreadInterruptedException konstruktorokat.

Konstruktorok

Name Description
ThreadInterruptedException()

Inicializálja az osztály új példányát ThreadInterruptedException alapértelmezett tulajdonságokkal.

ThreadInterruptedException(SerializationInfo, StreamingContext)
Elavult.

Inicializálja az ThreadInterruptedException osztály új példányát szerializált adatokkal.

ThreadInterruptedException(String, Exception)

Inicializálja az ThreadInterruptedException osztály új példányát egy megadott hibaüzenettel és a kivétel okaként szolgáló belső kivételre mutató hivatkozással.

ThreadInterruptedException(String)

Inicializálja az ThreadInterruptedException osztály új példányát egy megadott hibaüzenettel.

Tulajdonságok

Name Description
Data

Lekéri a kulcs-/érték párok gyűjteményét, amelyek további, felhasználó által definiált információkat biztosítanak a kivételről.

(Öröklődés forrása Exception)
HelpLink

Lekéri vagy beállítja a kivételhez társított súgófájlra mutató hivatkozást.

(Öröklődés forrása Exception)
HResult

Lekéri vagy beállítja a HRESULT-ot, egy kódolt numerikus értéket, amely egy adott kivételhez van hozzárendelve.

(Öröklődés forrása Exception)
InnerException

Lekéri az Exception aktuális kivételt okozó példányt.

(Öröklődés forrása Exception)
Message

Az aktuális kivételt leíró üzenet jelenik meg.

(Öröklődés forrása Exception)
Source

Lekéri vagy beállítja az alkalmazás vagy a hibát okozó objektum nevét.

(Öröklődés forrása Exception)
StackTrace

Lekéri a hívásverem közvetlen kereteinek sztringképét.

(Öröklődés forrása Exception)
TargetSite

Lekéri az aktuális kivételt okozó metódust.

(Öröklődés forrása Exception)

Metódusok

Name Description
Equals(Object)

Meghatározza, hogy a megadott objektum egyenlő-e az aktuális objektummal.

(Öröklődés forrása Object)
GetBaseException()

Ha egy származtatott osztály felül van bírálva, egy Exception vagy több későbbi kivétel kiváltó okát adja vissza.

(Öröklődés forrása Exception)
GetHashCode()

Ez az alapértelmezett kivonatoló függvény.

(Öröklődés forrása Object)
GetObjectData(SerializationInfo, StreamingContext)
Elavult.

Ha felül van bírálva egy származtatott osztályban, a SerializationInfo kivétel adatait adja meg.

(Öröklődés forrása Exception)
GetType()

Lekéri az aktuális példány futtatókörnyezeti típusát.

(Öröklődés forrása Exception)
MemberwiseClone()

Az aktuális Objectpéldány sekély másolatát hozza létre.

(Öröklődés forrása Object)
ToString()

Létrehozza és visszaadja az aktuális kivétel sztring-ábrázolását.

(Öröklődés forrása Exception)

esemény

Name Description
SerializeObjectState
Elavult.

Akkor fordul elő, ha a kivétel szerializálva van egy kivételállapot-objektum létrehozásához, amely szerializált adatokat tartalmaz a kivételről.

(Öröklődés forrása Exception)

A következőre érvényes:

Lásd még