ThreadAbortException ThreadAbortException ThreadAbortException Class
定义
命名空间:
System.Threading System.Threading System.Threading System.Threading
Assemblies:
mscorlib.dll, netstandard.dll, System.Threading.Thread.dll
当对 Abort(Object) 方法发出调用时引发的异常。The exception that is thrown when a call is made to the Abort(Object) method. 此类不能被继承。This class cannot be inherited.
本文内容
public ref class ThreadAbortException sealed : SystemException
C# 复制
[
System.Runtime.InteropServices.ComVisible(true)]
[
System.Serializable]
publicsealed
class
ThreadAbortException
:
SystemException
type ThreadAbortException = class
inherit SystemException
Public NotInheritable Class ThreadAbortException
Inherits SystemException
继承
ExceptionExceptionExceptionException
SystemExceptionSystemExceptionSystemExceptionSystemException
ThreadAbortExceptionThreadAbortExceptionThreadAbortExceptionThreadAbortException
属性
ComVisibleAttribute SerializableAttribute
示例
下面的示例演示正在中止线程。The following example demonstrates aborting a thread. 接收的线程ThreadAbortException
使用ResetAbort方法来取消中止请求,然后继续执行。The thread that receives the ThreadAbortException
uses the ResetAbort method to cancel the abort request and continue executing.
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." );
}
C# 复制
using System;
using System.Threading;
using System.Security.Permissions;
publicclass
ThreadWork
{
public static void DoWork()
{
try
{
for
(
inti=
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.");
}
}
classThreadAbortTest
{
public static void Main()
{
ThreadStart myThreadDelegate =
newThreadStart(ThreadWork.DoWork);
Thread myThread =
newThread(myThreadDelegate);
myThread.Start();
Thread.Sleep(
100);
Console.WriteLine(
"Main - aborting my thread.");
myThread.Abort();
myThread.Join();
Console.WriteLine(
"Main ending.");
}
}
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
此代码生成以下输出:This code produces the following output:
复制
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.
注解
当调用Abort方法来销毁线程时,公共语言运行时将引发ThreadAbortException。When a call is made to the Abort method to destroy a thread, the common language runtime throws a ThreadAbortException. ThreadAbortException 是一个特殊的异常可以被捕获,但它将自动被再次引发结束时catch
块。ThreadAbortException is a special exception that can be caught, but it will automatically be raised again at the end of the catch
block. 当引发此异常时,运行时执行所有finally
块结束线程之前。When this exception is raised, the runtime executes all the finally
blocks before ending the thread. 由于该线程可在中执行不受限制的计算finally块或调用Thread.ResetAbort若要取消 abort,则不能保证的线程将完全结束。Because the thread can do an unbounded computation in the finally
blocks or call Thread.ResetAbort to cancel the abort, there is no guarantee that the thread will ever end. 如果你想要等待,直到中止的线程已结束,则可以调用Thread.Join方法。If you want to wait until the aborted thread has ended, you can call the Thread.Join method. Join 是阻止调用,不会返回之前线程实际停止执行。Join is a blocking call that does not return until the thread actually stops executing.
备注
如果公共语言运行时 (CLR) 托管的可执行文件中的所有前台线程已都结束后停止后台线程,它不使用Thread.Abort。When the common language runtime (CLR) stops background threads after all foreground threads in a managed executable have ended, it does not use Thread.Abort. 因此,不能使用ThreadAbortException检测时由 CLR 正在终止后台线程。Therefore, you cannot use ThreadAbortException to detect when background threads are being terminated by the CLR.
ThreadAbortException 使用 HRESULT COR_E_THREADABORTED,其值 0x80131530。ThreadAbortException uses the HRESULT COR_E_THREADABORTED, which has the value 0x80131530.