Thread.Abort Thread.Abort Thread.Abort Thread.Abort Method

定義

於被叫用的所在執行緒中引發 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() Abort() Abort() 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) Abort(Object) Abort(Object) 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程式碼,也不會確定應用程式的狀態或任何應用程式和使用者狀態負責保留。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.

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

Abort() Abort() Abort() 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是可以由應用程式代碼catch攔截的特殊例外狀況,但除非呼叫,否則ResetAbort會在區塊結尾處重新擲回。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. 若要等到執行緒中止,您可以在Join Abort呼叫方法之後呼叫執行緒上的方法,但不保證會結束等候。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.

注意

如果要中止的Abort執行緒是在程式碼的受保護區域(例如catch區塊、 finally區塊或限制的執列區域)中,呼叫的執行緒可能會封鎖。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在已暫止的執行緒上呼叫ThreadStateException ,則會在呼叫Abort的執行緒中擲回,並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.

如果Abort在執行非受控碼的 managed 執行緒上呼叫ThreadAbortException ,就不會擲回,直到執行緒回到 managed 程式碼為止。If 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都同時出現,則可能會有一個呼叫來設定狀態資訊,而另一個呼叫則可Abort執行。If 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. 相關聯的ControlThread列舉:。Associated enumeration: ControlThread.

另請參閱

Abort(Object) Abort(Object) Abort(Object) 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 Object Object 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是可以由應用程式代碼catch攔截的特殊例外狀況,但除非呼叫,否則ResetAbort會在區塊結尾處重新擲回。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. 若要等到執行緒中止,您可以在Join Abort呼叫方法之後,線上程上呼叫方法,但不保證會結束等候。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.

注意

如果要中止的Abort執行緒是在程式碼的受保護區域(例如catch區塊、 finally區塊或限制的執列區域)中,呼叫的執行緒可能會封鎖。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在已暫止的執行緒上呼叫ThreadStateException ,則會在呼叫Abort的執行緒中擲回,並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.

如果Abort在執行非受控碼的 managed 執行緒上呼叫ThreadAbortException ,就不會擲回,直到執行緒回到 managed 程式碼為止。If 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都同時出現,則可能會有一個呼叫來設定狀態資訊,而另一個呼叫則可Abort執行。If 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. 相關聯的ControlThread列舉:。Associated enumeration: ControlThread.

另請參閱

適用於