ThreadAbortException ThreadAbortException ThreadAbortException Class

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.

本文内容

  1. 定义
  2. 示例
  3. 注解
  4. 属性
  5. 方法
  6. 事件
  7. 适用于
  8. 另请参阅
public ref class ThreadAbortException sealed : SystemException

C# 复制

[System.Runtime.InteropServices.ComVisible(true)]
[System.Serializable]
public sealed class ThreadAbortException : SystemException
type ThreadAbortException = class
    inherit SystemException
Public NotInheritable Class ThreadAbortException
Inherits SystemException

继承

ObjectObjectObjectObject

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;
 
public 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.");
    }
}
 
class ThreadAbortTest {
    public static void Main() {
        ThreadStart myThreadDelegate = new ThreadStart(ThreadWork.DoWork);
        Thread myThread = new Thread(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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值