抓一个ThreadAbortException时,这是怎么回事的隐藏投掷? [英] What's the deal with the hidden Throw when catching a ThreadAbortException?

查看:87
本文介绍了抓一个ThreadAbortException时,这是怎么回事的隐藏投掷?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要通过一本书,一般的C#开发,我已经来到了线程中止部分。

I'm going through a book of general c# development, and I've come to the thread abort section.

这本书说,沿线的东西,当你在另一个线程调用Thread.Abort的(),该线程将抛出一个ThreadAbortException,即使你设法剿它它会自动重新抛出它,除非你做了一些BS是一般的令人难以接受的。 。下面是提供的简单例子

The book says something along the lines that when you call Thread.Abort() on another thread, that thread will throw a ThreadAbortException, and even if you tried to supress it it would automatically rethrow it, unless you did some bs that's generally frowned upon. Here's the simple example offered.

using System;
using System.Threading;

public class EntryPoint
{
    private static void ThreadFunc()
    {
        ulong counter = 0;
        while (true)
        {
            try
            {
                Console.WriteLine("{0}", counter++);
            }
            catch (ThreadAbortException)
            {
                // Attempt to swallow the exception and continue.
                Console.WriteLine("Abort!");
             }
        }
    }

    static void Main()
    {
        try
        {
            Thread newThread = new Thread(new ThreadStart(EntryPoint.ThreadFunc));
            newThread.Start();
            Thread.Sleep(2000);

            // Abort the thread.
            newThread.Abort();

           // Wait for thread to finish.
           newThread.Join();
        }
       catch (Exception e)
       {
           Console.WriteLine(e.ToString());
       }
    }
}



书中说:

The book says:

当你的线程处理完中止异常,运行时隐在你的异常处理程序结束重新抛出它。这是因为如果你有自己重新抛出异常的一样。因此,任何外部异常处理程序或finally块仍将正常执行。在这个例子中,调用加入不会永远如最初预期的等待着。

When your thread finishes processing the abort exception, the runtime implicitly rethrows it at the end of your exception handler. It’s the same as if you had rethrown the exception yourself. Therefore, any outer exception handlers or finally blocks will still execute normally. In the example, the call to Join won’t be waiting forever as initially expected.

所以我缠Thread.Abort的()调用尝试捕捉并设置破发点,期待它打这个,考虑到文中说:任何外部异常处理程序或finally块仍将执行正常。但是它没有。我伤透我的大脑找出原因。

So i wrapped a try catch around the Thread.Abort() call and set a break point, expecting it to hit this, considering the text says "any outer exception handlers or finally blocks will still execute normally". BUT IT DOES NOT. I'm racking my brain to figure out why.

任何人有这是为什么不是这样的想法?事先书错了吗?

Anyone have any thoughts on why this isn't the case? Is the book wrong?

感谢。

推荐答案

的异常被抛出了被中止的线程上。一旦抛出的异常传播了该线程的调用堆栈,但它不会跳过到另一个线程,也不来电者 Thread.Abort的

The exception is thrown on the thread that is aborted. Once thrown an exception travels up the call stack of that thread, but it does not jump over to another thread, nor to the caller of Thread.Abort.

的说法是:

因此,任何外部异常处理程序或finally块仍将执行正常。

Therefore, any outer exception handlers or finally blocks will still execute normally.

这种说法的一个更好的测试是下面的代码:

A better test of this claim is the following code:

private static void ThreadFunc()
{
    ulong counter = 0;
    while (true)
    {
        try
        {
            try
            {
                Console.WriteLine("{0}", counter++);
            }
            catch (ThreadAbortException)
            {
                // Attempt to swallow the exception and continue.
                Console.WriteLine("Abort!");
            }
        }
        catch (ThreadAbortException)
        {
            Console.WriteLine("Do we get here?");
        }
    }
}

如果 ThreadAbortException 是一个正常的异常类型,我们不希望打行难道我们这里得到什么?,但我们做的。

If ThreadAbortException were a normal type of exception we would not expect to hit the line "Do we get here?", but we do.

这篇关于抓一个ThreadAbortException时,这是怎么回事的隐藏投掷?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆