Abort 方法 (Object)
.NET Framework 类库
Thread.Abort 方法 (Object)

在调用此方法的线程上引发 ThreadAbortException,以开始终止此线程并提供有关线程终止的异常信息的过程。调用此方法通常会终止线程。

命名空间:System.Threading
程序集:mscorlib(在 mscorlib.dll 中)

Visual Basic(声明)
Public Sub Abort ( _
    stateInfo As Object _
)
Visual Basic(用法)
Dim instance As Thread
Dim stateInfo As Object

instance.Abort(stateInfo)
C#
public void Abort (
    Object stateInfo
)
C++
public:
void Abort (
    Object^ stateInfo
)
J#
public void Abort (
    Object stateInfo
)
JScript
public function Abort (
    stateInfo : Object
)

参数

stateInfo

一个对象,它包含应用程序特定的信息(如状态),该信息可供正被中止的线程使用。

异常类型条件

SecurityException

调用方没有所要求的权限。

ThreadStateException

中止的线程当前被挂起。

在线程上调用此方法时,系统在线程中引发 ThreadAbortException 以中止它。ThreadAbortException 是一个可以由应用程序代码捕获的特殊异常,但除非调用 ResetAbort,否则会在 catch 块的结尾再次引发它。ResetAbort 取消要中止的请求,并阻止 ThreadAbortException 终止线程。未执行的 finally 块将在线程终止前执行。

Note注意

当线程对自身调用 Abort 时,效果类似于引发异常;ThreadAbortException 会立刻发生,并且结果是可预知的。但是,如果一个线程对另一个线程调用 Abort,则将中断运行的任何代码。静态构造函数有可能被中止。在极少数情况下,这可以防止在该应用程序域中创建该类的实例。在 .NET Framework 1.0 版和 1.1 版中,在 finally 块运行时线程可能会中止,在这种情况下,finally 块将被中止。

线程不一定会立即中止,或者根本不中止。如果线程在作为中止过程的一部分被调用的 finally 块中做非常大量的计算,从而无限期延迟中止操作,则会发生这种情况。若要在线程中止之前一直等待,可以在调用 Abort 方法后对该线程调用 Join 方法,但是不能保证等待会结束。

如果对尚未启动的线程调用 Abort,则当调用 Start 时该线程将中止。如果对被阻止或正在休眠的线程调用 Abort,则该线程被中断然后中止。

如果在已挂起的线程上调用 Abort,则将在调用 Abort 的线程中引发 ThreadStateException,并将 AbortRequested 添加到被中止的线程的 ThreadState 属性中。直到调用 Resume 后,才在挂起的线程中引发 ThreadAbortException

如果在执行非托管代码时线程忽略 ThreadAbortException,则当线程开始执行托管代码时,系统将再次引发 ThreadAbortException

如果同时出现两次对 Abort 的调用,则可能一个调用设置状态信息,而另一个调用执行 Abort。但是,应用程序无法区分这一点。

对线程调用了 Abort 后,线程状态包括 AbortRequested。成功调用 Abort 而使线程终止后,线程状态更改为 Stopped。如果有足够的权限,作为 Abort 目标的线程就可以使用 ResetAbort 方法取消中止操作。有关说明如何调用 ResetAbort 方法的示例,请参见 ThreadAbortException 类。

下面的代码示例说明如何将信息传递给正被中止的线程。

Visual Basic
Imports System
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
C#
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);
        }
    }
}
C++
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." );
}
J#
import System.*;
import System.Threading.*;
import System.Threading.Thread;    

class Test
{
    public static void main(String[] args)
    {
        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.");
    } //main

    static void TestMethod()
    {
        try {
            while (true) {
                Console.WriteLine("New thread running.");
                Thread.Sleep(1000);
            }
        }
        catch (ThreadAbortException abortException) {
            Console.WriteLine((System.String)(abortException.
                get_ExceptionState()));
        }
    } //TestMethod
} //Test

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
社区内容   什么是社区内容?
添加新内容 RSS  批注
Processing
Page view tracker