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) メンバはすべて、スレッド セーフです。インスタンス メンバ場合は、スレッド セーフであるとは限りません。
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照



英和和英テキスト翻訳>> Weblio翻訳
英語⇒日本語日本語⇒英語
  

辞書ショートカット

すべての辞書の索引

「ThreadAbortException クラス」の関連用語

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

   

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



ThreadAbortException クラスのページの著作権
Weblio 辞書 情報提供元は 参加元一覧 にて確認できます。

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

©2025 GRAS Group, Inc.RSS