다음을 통해 공유


ThreadAbortException 클래스

Abort 메서드를 호출할 때 throw되는 예외입니다. 이 클래스는 상속될 수 없습니다.

네임스페이스: 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을 throw합니다. ThreadAbortException은 catch될 수 있는 특수한 예외이지만 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) 멤버는 스레드로부터 안전합니다. 인터페이스 멤버는 스레드로부터 안전하지 않습니다.

플랫폼

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

.NET Framework에서 모든 플래폼의 모든 버전을 지원하지는 않습니다. 지원되는 버전의 목록은 시스템 요구 사항을 참조하십시오.

버전 정보

.NET Framework

2.0, 1.1, 1.0에서 지원

.NET Compact Framework

2.0에서 지원

참고 항목

참조

ThreadAbortException 멤버
System.Threading 네임스페이스
Thread 클래스
Thread.Abort

기타 리소스

스레드 삭제