ThreadAbortException クラス

Abort メソッドが呼び出されるときにスローされる例外。このクラスは継承できません。

この型のすべてのメンバの一覧については、ThreadAbortException メンバ を参照してください。

System.Object
   System.Exception
      System.SystemException
         System.Threading.ThreadAbortException

<Serializable>
NotInheritable Public Class ThreadAbortException   Inherits SystemException
[C#]
[Serializable]
public sealed class ThreadAbortException : SystemException
[C++]
[Serializable]
public __gc __sealed class ThreadAbortException : public   SystemException
[JScript]
public
   Serializable
class ThreadAbortException extends SystemException

スレッドセーフ

この型の public static (Visual Basicでは Shared) のすべてのメンバは、マルチスレッド操作で安全に使用できます。インスタンスのメンバの場合は、スレッドセーフであるとは限りません。

解説

スレッドを破棄するために Abort メソッドが呼び出されるときに、共通言語ランタイムは ThreadAbortException をスローします。 ThreadAbortException は、キャッチできても、 catch ブロックの末尾でもう一度自動的に発生する特殊な例外です。この例外が発生すると、ランタイムは、スレッドを強制終了する前に finally ブロックをすべて実行します。スレッドは、 finally ブロックで無制限に計算を実行できるため、スレッドを中止させるために Join メソッドを呼び出す必要があります。 Join は、スレッドが実際に実行を停止するまで戻らないブロッキング呼び出しです。

ThreadAbortException は、値 0x80131530 を保持する HRESULT COR_E_THREADABORTED を使用します。

使用例

[Visual Basic, C#, C++] スレッドを中止する例を次に示します。 ThreadAbortException を受け取ったスレッドは、 ResetAbort メソッドを使用して中止要求をキャンセルし、実行を継続します。

 
Imports System
Imports System.Threading
Imports System.Security.Permissions


Public Class ThreadWork
   Public Shared Sub DoWork()
      Try
         Dim i As Integer
         For i = 0 To 99
            Console.WriteLine("Thread - working.")
            Thread.Sleep(100)
         Next i
      Catch e As ThreadAbortException
         Console.WriteLine("Thread - caught ThreadAbortException - resetting.")
         Console.WriteLine("Exception message: {0}", e.Message)
         Thread.ResetAbort()
      End Try
      Console.WriteLine("Thread - still alive and working.")
      Thread.Sleep(1000)
      Console.WriteLine("Thread - finished working.")
   End Sub 'DoWork
End Class 'ThreadWork


Class ThreadAbortTest
   Public Shared Sub Main()
      Dim myThreadDelegate As New ThreadStart(AddressOf ThreadWork.DoWork)
      Dim myThread As New Thread(myThreadDelegate)
      myThread.Start()
      Thread.Sleep(100)
      Console.WriteLine("Main - aborting my thread.")
      myThread.Abort()
      myThread.Join()
      Console.WriteLine("Main ending.")
   End Sub 'Main
End Class 'ThreadAbortTest

[C#] 
using System;
using System.Threading;
using System.Security.Permissions;

public class ThreadWork {
    public static void DoWork() {
        try {
            for(int i=0; i<100; i++) {
                Console.WriteLine("Thread - working."); 
                Thread.Sleep(100);
            }
        }
        catch(ThreadAbortException e) {
            Console.WriteLine("Thread - caught ThreadAbortException - resetting.");
            Console.WriteLine("Exception message: {0}", e.Message);
            Thread.ResetAbort();
        }
        Console.WriteLine("Thread - still alive and working."); 
        Thread.Sleep(1000);
        Console.WriteLine("Thread - finished working.");
    }
}

class ThreadAbortTest {
    public static void Main() {
        ThreadStart myThreadDelegate = new ThreadStart(ThreadWork.DoWork);
        Thread myThread = new Thread(myThreadDelegate);
        myThread.Start();
        Thread.Sleep(100);
        Console.WriteLine("Main - aborting my thread.");
        myThread.Abort();
        myThread.Join();
        Console.WriteLine("Main ending."); 
    }
}

[C++] 
#using <mscorlib.dll>

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

__gc class ThreadWork 
{
public:
    static void DoWork() 
    {
        try 
        {
            for (int i=0; i<100; i++) 
            {
                Console::WriteLine(S"Thread - working."); 
                Thread::Sleep(100);
            }
        } 
        catch (ThreadAbortException* e) 
        {
            Console::WriteLine(S"Thread - caught ThreadAbortException - resetting.");
            Console::WriteLine(S"Exception message: {0}", e->Message);
            Thread::ResetAbort();
        }
        Console::WriteLine(S"Thread - still alive and working."); 
        Thread::Sleep(1000);
        Console::WriteLine(S"Thread - finished working.");
    }
};

int main() 
{
    ThreadStart* myThreadDelegate = new ThreadStart(0, ThreadWork::DoWork);
    Thread* myThread = new Thread(myThreadDelegate);
    myThread->Start();
    Thread::Sleep(100);
    Console::WriteLine(S"Main - aborting my thread.");
    myThread->Abort();
    myThread->Join();
    Console::WriteLine(S"Main ending."); 
}

[Visual Basic, C#, C++] このコードによって、次の出力が生成されます。

Thread - working.
Main - aborting my thread.
Thread - caught ThreadAbortException - resetting.
Exception message: Thread was being aborted.
Thread - still alive and working.
Thread - finished working.
Main ending.

[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

必要条件

名前空間: System.Threading

プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ

アセンブリ: Mscorlib (Mscorlib.dll 内)

参照

ThreadAbortException メンバ | System.Threading 名前空間 | Thread | Thread.Abort | スレッドの破棄