Thread.Abort 方法

定義

於被叫用的所在執行緒中引發 ThreadAbortException,開始處理執行緒的結束作業。Raises a ThreadAbortException in the thread on which it is invoked, to begin the process of terminating the thread. 呼叫這個方法通常會結束執行緒。Calling this method usually terminates the thread.

多載

Abort()

於被叫用的所在執行緒中引發 ThreadAbortException,開始處理執行緒的結束作業。Raises a ThreadAbortException in the thread on which it is invoked, to begin the process of terminating the thread. 呼叫這個方法通常會結束執行緒。Calling this method usually terminates the thread.

Abort(Object)

於被叫用的所在執行緒中引發 ThreadAbortException,開始結束執行緒的處理作業,同時也提供執行緒結束的相關例外狀況資訊。Raises a ThreadAbortException in the thread on which it is invoked, to begin the process of terminating the thread while also providing exception information about the thread termination. 呼叫這個方法通常會結束執行緒。Calling this method usually terminates the thread.

備註

重要

應謹慎使用 Thread.Abort 方法。The Thread.Abort method should be used with caution. 特別是當您呼叫它來中止目前線程以外的執行緒時,您不知道擲回 ThreadAbortException 或無法執行的程式碼,也不會確定您應用程式的狀態,或是回應的應用程式和使用者狀態。保留的 nsible。Particularly when you call it to abort a thread other than the current thread, you do not know what code has executed or failed to execute when the ThreadAbortException is thrown, nor can you be certain of the state of your application or any application and user state that it is responsible for preserving. 例如,呼叫 Thread.Abort 可能會使靜態的執行程式無法執行或防止非受控資源的釋放。For example, calling Thread.Abort may prevent static constructors from executing or prevent the release of unmanaged resources.

請注意,.NET Core 不支援 Thread.Abort 方法。Note that the Thread.Abort method is not supported on .NET Core.

Abort()

於被叫用的所在執行緒中引發 ThreadAbortException,開始處理執行緒的結束作業。Raises a ThreadAbortException in the thread on which it is invoked, to begin the process of terminating the thread. 呼叫這個方法通常會結束執行緒。Calling this method usually terminates the thread.

public:
 void Abort();
public void Abort ();
member this.Abort : unit -> unit
Public Sub Abort ()

例外狀況

僅限 .NET Core:不支援此成員。.NET Core only: This member is not supported.

呼叫端沒有必要的使用權限。The caller does not have the required permission.

要中止的執行緒目前已暫止。The thread that is being aborted is currently suspended.

備註

線上程上叫用這個方法時,系統會線上程中擲回 ThreadAbortException,以將它中止。When this method is invoked on a thread, the system throws a ThreadAbortException in the thread to abort it. ThreadAbortException 是可由應用程式代碼攔截的特殊例外狀況,但除非呼叫 ResetAbort,否則會在 catch 區塊結尾處重新擲回。ThreadAbortException is a special exception that can be caught by application code, but is re-thrown at the end of the catch block unless ResetAbort is called. ResetAbort 會取消中止的要求,並防止 ThreadAbortException 終止執行緒。ResetAbort cancels the request to abort, and prevents the ThreadAbortException from terminating the thread. 未執行 finally 區塊會線上程中止之前執行。Unexecuted finally blocks are executed before the thread is aborted.

注意

當執行緒本身呼叫 Abort 時,其效果類似于擲回例外狀況。ThreadAbortException 會立即發生,而且結果是可預測的。When a thread calls Abort on itself, the effect is similar to throwing an exception; the ThreadAbortException happens immediately, and the result is predictable. 不過,如果某個執行緒在另一個執行緒上呼叫 Abort,則中止會中斷任何程式碼正在執行。However, if one thread calls Abort on another thread, the abort interrupts whatever code is running. 也有可能會中止靜態的函式。There is also a chance that a static constructor could be aborted. 在罕見的情況下,這可能會讓該類別的實例無法在該應用程式域中建立。In rare cases, this might prevent instances of that class from being created in that application domain. 在 .NET Framework 版本1.0 和1.1 中,執行緒可能會在 finally 區塊正在執行時中止,在此情況下,finally 區塊會中止。In the .NET Framework versions 1.0 and 1.1, there is a chance the thread could abort while a finally block is running, in which case the finally block is aborted.

不保證會立即中止執行緒,或完全無法終止。The thread is not guaranteed to abort immediately, or at all. 如果執行緒在做為中止程式之一部分呼叫的 finally 區塊中執行未限制的計算量,就會發生這種情況,因此會無限期地延遲中止。This situation can occur if a thread does an unbounded amount of computation in the finally blocks that are called as part of the abort procedure, thereby indefinitely delaying the abort. 若要等到執行緒中止,您可以在呼叫 Abort 方法之後,線上程上呼叫 Join 方法,但不保證會結束等候。To wait until a thread has aborted, you can call the Join method on the thread after calling the Abort method, but there is no guarantee the wait will end.

注意

如果要中止的執行緒是在程式碼的受保護區域(例如 catch 區塊、finally 區塊或限制的執列區域)中,則呼叫 Abort 的執行緒可能會封鎖。The thread that calls Abort might block if the thread that is being aborted is in a protected region of code, such as a catch block, finally block, or constrained execution region. 如果呼叫 Abort 的執行緒持有已中止執行緒所需的鎖定,則可能會發生鎖死。If the thread that calls Abort holds a lock that the aborted thread requires, a deadlock can occur.

如果在尚未啟動的執行緒上呼叫 Abort,執行緒會在呼叫 Start 時中止。If Abort is called on a thread that has not been started, the thread will abort when Start is called. 如果在已封鎖或睡眠中的執行緒上呼叫 Abort,則執行緒會中斷,然後中止。If Abort is called on a thread that is blocked or is sleeping, the thread is interrupted and then aborted.

如果在已暫止的執行緒上呼叫 Abort,則會在呼叫 Abort 的執行緒中擲回 ThreadStateException,並將 AbortRequested 新增至要中止之執行緒的 ThreadState 屬性。If Abort is called on a thread that has been suspended, a ThreadStateException is thrown in the thread that called Abort, and AbortRequested is added to the ThreadState property of the thread being aborted. 在呼叫 Resume 之前,不會在暫止的執行緒中擲回 ThreadAbortExceptionA ThreadAbortException is not thrown in the suspended thread until Resume is called.

如果在執行非受控碼的 managed 執行緒上呼叫 Abort,則線上程回到 managed 程式碼之前,不會擲回 ThreadAbortExceptionIf Abort is called on a managed thread while it is executing unmanaged code, a ThreadAbortException is not thrown until the thread returns to managed code.

如果兩個 Abort 的呼叫同時出現,則一個呼叫可能會設定狀態資訊,而另一個呼叫則會執行 AbortIf two calls to Abort come at the same time, it is possible for one call to set the state information and the other call to execute the Abort. 不過,應用程式無法偵測到這種情況。However, an application cannot detect this situation.

線上程上叫用 Abort 之後,執行緒的狀態就會包含 AbortRequestedAfter Abort is invoked on a thread, the state of the thread includes AbortRequested. 成功呼叫 Abort 後,執行緒終止之後,執行緒的狀態就會變更為 StoppedAfter the thread has terminated as a result of a successful call to Abort, the state of the thread is changed to Stopped. 有足夠的許可權,做為 Abort 目標的執行緒可以使用 ResetAbort 方法取消中止。With sufficient permissions, a thread that is the target of an Abort can cancel the abort using the ResetAbort method. 如需示範如何呼叫 ResetAbort 方法的範例,請參閱 ThreadAbortException 類別。For an example that demonstrates calling the ResetAbort method, see the ThreadAbortException class.

安全性

SecurityPermission
適用于執行緒的 advanced 作業。for advanced operations on threads. 關聯的列舉型別:ControlThreadAssociated enumeration: ControlThread.

另請參閱

Abort(Object)

於被叫用的所在執行緒中引發 ThreadAbortException,開始結束執行緒的處理作業,同時也提供執行緒結束的相關例外狀況資訊。Raises a ThreadAbortException in the thread on which it is invoked, to begin the process of terminating the thread while also providing exception information about the thread termination. 呼叫這個方法通常會結束執行緒。Calling this method usually terminates the thread.

public:
 void Abort(System::Object ^ stateInfo);
public void Abort (object stateInfo);
member this.Abort : obj -> unit
Public Sub Abort (stateInfo As Object)

參數

stateInfo
Object

包含特定應用程式資訊 (例如狀態) 的物件,正在中止的執行緒可以使用這個物件。An object that contains application-specific information, such as state, which can be used by the thread being aborted.

例外狀況

僅限 .NET Core:不支援此成員。.NET Core only: This member is not supported.

呼叫端沒有必要的使用權限。The caller does not have the required permission.

要中止的執行緒目前已暫止。The thread that is being aborted is currently suspended.

範例

下列程式碼範例示範如何將資訊傳遞給正在中止的執行緒。The following code example shows how to pass information to a thread that is being aborted.

using namespace System;
using namespace System::Threading;
ref class Test
{
private:
   Test(){}


public:
   static void TestMethod()
   {
      try
      {
         while ( true )
         {
            Console::WriteLine( "New thread running." );
            Thread::Sleep( 1000 );
         }
      }
      catch ( ThreadAbortException^ abortException ) 
      {
         Console::WriteLine( dynamic_cast<String^>(abortException->ExceptionState) );
      }

   }

};

int main()
{
   Thread^ newThread = gcnew Thread( gcnew ThreadStart( &Test::TestMethod ) );
   newThread->Start();
   Thread::Sleep( 1000 );
   
   // Abort newThread.
   Console::WriteLine( "Main aborting new thread." );
   newThread->Abort( "Information from main." );
   
   // Wait for the thread to terminate.
   newThread->Join();
   Console::WriteLine( "New thread terminated - main exiting." );
}

using System;
using System.Threading;

class Test
{
    public static void Main()
    {
        Thread newThread  = new Thread(new ThreadStart(TestMethod));
        newThread.Start();
        Thread.Sleep(1000);

        // Abort newThread.
        Console.WriteLine("Main aborting new thread.");
        newThread.Abort("Information from Main.");

        // Wait for the thread to terminate.
        newThread.Join();
        Console.WriteLine("New thread terminated - Main exiting.");
    }

    static void TestMethod()
    {
        try
        {
            while(true)
            {
                Console.WriteLine("New thread running.");
                Thread.Sleep(1000);
            }
        }
        catch(ThreadAbortException abortException)
        {
            Console.WriteLine((string)abortException.ExceptionState);
        }
    }
}
Imports System.Threading

Public Class Test

    <MTAThread> _
    Shared Sub Main()
        Dim newThread As New Thread(AddressOf TestMethod)
        newThread.Start()
        Thread.Sleep(1000)

        ' Abort newThread.
        Console.WriteLine("Main aborting new thread.")
        newThread.Abort("Information from Main.")

        ' Wait for the thread to terminate.
        newThread.Join()
        Console.WriteLine("New thread terminated - Main exiting.")
    End Sub

    Shared Sub TestMethod()
        Try
            While True
                Console.WriteLine("New thread running.")
                Thread.Sleep(1000)
            End While
        Catch abortException As ThreadAbortException
            Console.WriteLine( _
                CType(abortException.ExceptionState, String))
        End Try
    End Sub

End Class

備註

線上程上叫用這個方法時,系統會線上程中擲回 ThreadAbortException,以將它中止。When this method is invoked on a thread, the system throws a ThreadAbortException in the thread to abort it. ThreadAbortException 是可由應用程式代碼攔截的特殊例外狀況,但除非呼叫 ResetAbort,否則會在 catch 區塊結尾處重新擲回。ThreadAbortException is a special exception that can be caught by application code, but is re-thrown at the end of the catch block unless ResetAbort is called. ResetAbort 會取消中止的要求,並防止 ThreadAbortException 終止執行緒。ResetAbort cancels the request to abort, and prevents the ThreadAbortException from terminating the thread. 未執行 finally 區塊會線上程中止之前執行。Unexecuted finally blocks are executed before the thread is aborted.

注意

當執行緒本身呼叫 Abort 時,其效果類似于擲回例外狀況。ThreadAbortException 會立即發生,而且結果是可預測的。When a thread calls Abort on itself, the effect is similar to throwing an exception; the ThreadAbortException happens immediately, and the result is predictable. 不過,如果某個執行緒在另一個執行緒上呼叫 Abort,則中止會中斷任何程式碼正在執行。However, if one thread calls Abort on another thread, the abort interrupts whatever code is running. 有可能會中止靜態的函式。There is a chance that a static constructor could be aborted. 在罕見的情況下,這可能會讓該類別的實例無法在該應用程式域中建立。In rare cases, this might prevent instances of that class from being created in that application domain. 在 .NET Framework 版本1.0 和1.1 中,執行緒可能會在 finally 區塊正在執行時中止,在此情況下,finally 區塊會中止。In the .NET Framework versions 1.0 and 1.1, there is a chance the thread could abort while a finally block is running, in which case the finally block is aborted.

不保證會立即中止執行緒,或完全無法終止。The thread is not guaranteed to abort immediately, or at all. 如果執行緒在做為中止程式之一部分呼叫的 finally 區塊中執行未限制的計算量,就會發生這種情況,因此會無限期地延遲中止。This situation can occur if a thread does an unbounded amount of computation in the finally blocks that are called as part of the abort procedure, thereby indefinitely delaying the abort. 若要等到執行緒中止,您可以在呼叫 Abort 方法之後,線上程上呼叫 Join 方法,但不保證會結束等候。To wait until a thread has aborted, you can call the Join method on the thread after calling the Abort method, but there is no guarantee that the wait will end.

注意

如果要中止的執行緒是在程式碼的受保護區域(例如 catch 區塊、finally 區塊或限制的執列區域)中,則呼叫 Abort 的執行緒可能會封鎖。The thread that calls Abort might block if the thread that is being aborted is in a protected region of code, such as a catch block, finally block, or constrained execution region. 如果呼叫 Abort 的執行緒持有已中止執行緒所需的鎖定,則可能會發生鎖死。If the thread that calls Abort holds a lock that the aborted thread requires, a deadlock can occur.

如果在尚未啟動的執行緒上呼叫 Abort,執行緒會在呼叫 Start 時中止。If Abort is called on a thread that has not been started, the thread will abort when Start is called. 如果在已封鎖或睡眠中的執行緒上呼叫 Abort,則執行緒會中斷,然後中止。If Abort is called on a thread that is blocked or is sleeping, the thread is interrupted and then aborted.

如果在已暫止的執行緒上呼叫 Abort,則會在呼叫 Abort 的執行緒中擲回 ThreadStateException,並將 AbortRequested 新增至要中止之執行緒的 ThreadState 屬性。If Abort is called on a thread that has been suspended, a ThreadStateException is thrown in the thread that called Abort, and AbortRequested is added to the ThreadState property of the thread being aborted. 在呼叫 Resume 之前,不會在暫止的執行緒中擲回 ThreadAbortExceptionA ThreadAbortException is not thrown in the suspended thread until Resume is called.

如果在執行非受控碼的 managed 執行緒上呼叫 Abort,則線上程回到 managed 程式碼之前,不會擲回 ThreadAbortExceptionIf Abort is called on a managed thread while it is executing unmanaged code, a ThreadAbortException is not thrown until the thread returns to managed code.

如果兩個 Abort 的呼叫同時出現,則一個呼叫可能會設定狀態資訊,而另一個呼叫則會執行 AbortIf two calls to Abort come at the same time, it is possible for one call to set the state information and the other call to execute the Abort. 不過,應用程式無法偵測到這種情況。However, an application cannot detect this situation.

線上程上叫用 Abort 之後,執行緒的狀態就會包含 AbortRequestedAfter Abort is invoked on a thread, the state of the thread includes AbortRequested. 成功呼叫 Abort 後,執行緒終止之後,執行緒的狀態就會變更為 StoppedAfter the thread has terminated as a result of a successful call to Abort, the state of the thread is changed to Stopped. 有足夠的許可權,做為 Abort 目標的執行緒可以使用 ResetAbort 方法取消中止。With sufficient permissions, a thread that is the target of an Abort can cancel the abort using the ResetAbort method. 如需示範如何呼叫 ResetAbort 方法的範例,請參閱 ThreadAbortException 類別。For an example that demonstrates calling the ResetAbort method, see the ThreadAbortException class.

安全性

SecurityPermission
適用于執行緒的 advanced 作業。for advanced operations on threads. 關聯的列舉型別:ControlThreadAssociated enumeration: ControlThread.

另請參閱

適用於