스레드 삭제

업데이트: 2007년 11월

Abort 메서드는 관리되는 스레드를 영구적으로 중지하는 데 사용됩니다. Abort를 호출하면 공용 언어 런타임에서 대상 스레드가 catch할 수 있는 ThreadAbortException을 throw합니다. 자세한 내용은 Thread.Abort를 참조하십시오.

참고:

스레드에서 해당 Abort 메서드가 호출될 때 비관리 코드를 실행하는 경우 런타임에서 해당 코드를 ThreadState.AbortRequested로 만듭니다. 스레드가 관리 코드로 되돌아가면 예외가 throw됩니다.

일단 스레드가 중단되면 다시 시작할 수 없습니다.

Abort 메서드로 인해 스레드가 즉시 중단되지는 않습니다. 대상 스레드가 ThreadAbortException을 catch하고 finally 블록에서 임의의 코드 크기를 실행할 수 있기 때문입니다. 스레드가 끝날 때까지 기다려야 하는 경우 Thread.Join을 호출할 수 있습니다. Thread.Join은 스레드 실행이 실제로 중단되거나 선택적 제한 시간 간격이 경과될 때까지 반환되지 않는 차단 호출입니다. 중단된 스레드는 ResetAbort 메서드를 호출하거나 finally 블록에서 바인딩되지 않은 처리를 수행할 수 있으므로 제한 시간을 지정하지 않으면 해당 대기가 끝나지 않을 수 있습니다.

Thread.Join 메서드에 대한 호출을 기다리는 스레드는 Thread.Interrupt를 호출하는 다른 스레드에 의해 중단될 수 있습니다.

ThreadAbortException 처리

사용자 코드에서 Abort를 호출하거나 스레드가 실행 중인 응용 프로그램 도메인을 언로드하여 스레드가 중단될 수 있는 경우(AppDomain.UnloadThread.Abort를 사용하여 스레드 종료) 해당 스레드는 다음 코드와 같이 ThreadAbortException을 처리하고 finally 절에서 마지막 처리를 수행해야 합니다.

Try
    ' Code that is executing when the thread is aborted.
Catch ex As ThreadAbortException
    ' Clean-up code can go here.
    ' If there is no Finally clause, ThreadAbortException is
    ' re-thrown by the system at the end of the Catch clause. 
Finally
    ' Clean-up code can go here.
End Try
' Do not put clean-up code here, because the exception 
' is rethrown at the end of the Finally clause.
try 
{
    // Code that is executing when the thread is aborted.
} 
catch (ThreadAbortException ex) 
{
    // Clean-up code can go here.
    // If there is no Finally clause, ThreadAbortException is
    // re-thrown by the system at the end of the Catch clause. 
}
// Do not put clean-up code here, because the exception 
// is rethrown at the end of the Finally clause.

finally 절의 끝에서 또는 finally 절이 없는 경우 catch 절의 끝에서 시스템에서 ThreadAbortException이 다시 throw되므로 정리 코드는 catch 절 또는 finally 절에 있어야 합니다.

Thread.ResetAbort 메서드를 사용하면 시스템에서 예외를 다시 throw하지 않도록 할 수 있습니다. 그러나 이 방법은 사용자 코드에서 ThreadAbortException이 발생한 경우에만 사용해야 합니다.

참고 항목

참조

ThreadAbortException

Thread

기타 리소스

스레드 및 스레딩 사용