ThreadAbortExceptionとは? わかりやすく解説

ThreadAbortException クラス

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

名前空間: System.Threading
アセンブリ: mscorlib (mscorlib.dll 内)
構文構文

<SerializableAttribute> _
<ComVisibleAttribute(True)> _
Public NotInheritable Class
 ThreadAbortException
    Inherits SystemException
Dim instance As ThreadAbortException
[SerializableAttribute] 
[ComVisibleAttribute(true)] 
public sealed class ThreadAbortException :
 SystemException
[SerializableAttribute] 
[ComVisibleAttribute(true)] 
public ref class ThreadAbortException sealed
 : public SystemException
/** @attribute SerializableAttribute() */ 
/** @attribute ComVisibleAttribute(true) */ 
public final class ThreadAbortException extends
 SystemException
SerializableAttribute 
ComVisibleAttribute(true) 
public final class ThreadAbortException extends
 SystemException
解説解説

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

メモメモ

共通言語ランタイム (CLR) は、マネージ実行可能ファイル内のすべてのフォアグラウンド スレッドが終了した後でバックグラウンド スレッドを中止するときに、System.Threading.Thread.Abort を使用しません。したがって、ThreadAbortException を使用して CLR によるバックグラウンド スレッドの終了を検出することはできません。

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

メモメモ

継承された Data プロパティの値は常に null 参照 (Visual Basic では Nothing) です。

使用例使用例

スレッドを中止する例を次に示します。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
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."); 
    }
}
using namespace System;
using namespace System::Threading;
using namespace System::Security::Permissions;
ref 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." );
   }

};

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

import System.*;
import System.Threading.*;
import System.Security.Permissions.*;

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

class ThreadAbortTest
{
    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(100);
        Console.WriteLine("main - aborting my thread.");
        myThread.Abort();
        myThread.Join();
        Console.WriteLine("main ending.");
    } //main
} //ThreadAbortTest

このコードによって、次の出力が生成されます。

 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.
継承階層継承階層
System.Object
   System.Exception
     System.SystemException
      System.Threading.ThreadAbortException
スレッド セーフスレッド セーフ
この型の public static (Visual Basic では Shared) メンバはすべて、スレッド セーフです。インスタンス メンバの場合は、スレッド セーフであるとは限りません。
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照

ThreadAbortException プロパティ


ThreadAbortException メソッド


ThreadAbortException メンバ

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

ThreadAbortException データ型で公開されるメンバを以下の表に示します。


パブリック プロパティパブリック プロパティ
パブリック メソッドパブリック メソッド
参照参照

関連項目

ThreadAbortException クラス
System.Threading 名前空間
Thread クラス
Thread.Abort

その他の技術情報

スレッドの破棄



英和和英テキスト翻訳

英語⇒日本語日本語⇒英語

辞書ショートカット

すべての辞書の索引

「ThreadAbortException」の関連用語

ThreadAbortExceptionのお隣キーワード
検索ランキング

   

英語⇒日本語
日本語⇒英語
   



ThreadAbortExceptionのページの著作権

   
日本マイクロソフト株式会社日本マイクロソフト株式会社
© 2026 Microsoft.All rights reserved.

©2026 GRAS Group, Inc.RSS