简体   繁体   English

C#thread中止异常

[英]C# thread abort exception

What happens after a thread.Abort() ?? 在thread.Abort()之后会发生什么?

Say i have: 说我有:

Thread mWorker = new Thread(new ThreadStart(this.run));
..
mWorker.Start();

**where**

private void run() 
{
      Logger.d(TAG, "run()");

      ...
      try {
        while (this.mRunning){
          ...
        }
      } catch (ThreadAbortException tae){
           Logger.e(TAG,"some msg", tae);
           this.doSomething();
      } catch (IOException ioe){
           Logger.e(TAG,"some msg", ioe);
           this.doSomething();
      } catch (Exception e){
           Logger.e(TAG,"some msg", e);
           this.doSomething();
      } finally {
            gracefoulyClose();
      }

      Logger.d(TAG, "run() - ended");
}

Thread is more complex.. but the esential is displayed here. 线程更复杂..但这里显示的是必要的。 So what happens when Abort() gets called? 那么当Abort()被调用时会发生什么? will my catch work and continue with the call of doSomething()? 我的捕获工作会继续doSomething()的调用吗?

Because i still receive in console: 因为我还在控制台收到:

A first chance exception of type 'System.Threading.ThreadAbortException' occurred in mscorlib.dll
An exception of type 'System.Threading.ThreadAbortException' occurred in mscorlib.dll but was not handled in user code

But i do have a catch for that. 但我确实有一个问题。 Don't I ?? 不是吗?

From the doc : 来自doc

When a call is made to the Abort method to destroy a thread, the common language runtime throws a ThreadAbortException. 当调用Abort方法来销毁线程时,公共语言运行库会抛出ThreadAbortException。 ThreadAbortException is a special exception that can be caught, but it will automatically be raised again at the end of the catch block . ThreadAbortException是一个可以捕获的特殊异常,但它会在catch块的末尾自动再次引发 When this exception is raised, the runtime executes all the finally blocks before ending the thread . 引发此异常运行时会在结束线程之前执行所有finally块 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. 因为线程可以在finally块中执行无限制计算或调用Thread.ResetAbort来取消中止,所以无法保证线程将永远结束。 If you want to wait until the aborted thread has ended, you can call the Thread.Join method. 如果要等到中止的线程结束,可以调用Thread.Join方法。 Join is a blocking call that does not return until the thread actually stops executing. Join是一个阻塞调用,在线程实际停止执行之前不会返回。

So in other words, after your catch block for the ThreadAbortException executes, the exception is re-raised, so your last logger line (eg Logger.d(TAG, "run() - ended") ) never executes. 换句话说,在执行ThreadAbortException catch块之后,会重新引发异常,因此您的最后一个记录器行(例如Logger.d(TAG, "run() - ended") )永远不会执行。 But since the call to this.doSoemthing is in the catch block for the ThreadAbortException , it will execute. 但由于呼叫this.doSoemthing是对catch块ThreadAbortException它将执行。

Note also that, your finally block does execute (refer to doc above). 另请注意,您的finally确实执行(请参阅上面的doc)。

If you are using some where in code response.redirect(); 如果您在代码response.redirect();中使用某些位置response.redirect(); then it will internally run thread.abort(); 那么它将在内部运行thread.abort(); so it will throw exception.Instead of that you can use Response.Redirect(url,false); 所以它会抛出异常。相反,你可以使用Response.Redirect(url,false);

You are getting a ThreadAbortException because your context is exiting before the thread is finished running. 您收到ThreadAbortException,因为在线程完成运行之前您的上下文正在退出。 You need to wait on the thread to complete before you exit. 在退出之前,您需要等待线程完成。 If you want to exit, you need to make sure that your thread can receive a signal (and act upon it) that your program wishes to end, and then your code that manages program execution must wait on the thread to complete: 如果要退出,则需要确保线程可以接收程序希望结束的信号(并对其执行操作),然后管理程序执行的代码必须等待线程完成:

if (mThread != null && mThread.IsAlive) {
    mThread.Join();
}

Use the overload with the timeout if you're worried about the thread never exiting, and kill the thread explicitly if you hit the timer. 如果您担心线程永不退出,请使用带超时的重载,如果您点击计时器,则显式终止该线程。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM