C# 到 CurrentThread.Abort 或不到 CurrentThread.Abort

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/321779/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 23:09:54  来源:igfitidea点击:

To CurrentThread.Abort or not to CurrentThread.Abort

c#.netmultithreading

提问by Mike Daniels

I've seen a number of examples that have a thread procedure that looks like this.

我看过许多示例,它们的线程过程如下所示。

    private void ThreadProc()
    {
        while (serviceStarted)
        {
            // do some work

            Thread.Sleep(new TimeSpan(0, 0, 5));
        }

        Thread.CurrentThread.Abort();
    }

Is the Abort()really necessary at the end?

最后Abort()真的有必要吗?

There are number of arguments against calling Abort()

有许多反对调用的论点 Abort()

  1. Once the procedure exited - it is expected it has already cleaned up after itself.
  2. Calling Abort()throws an exception, which is generally more resource intensive than just exiting a procedure.
  1. 一旦程序退出 - 预计它已经自行清理干净。
  2. 调用Abort()会引发异常,这通常比仅退出过程更占用资源。

I'd like to read an explanation for why this is or isn't a good practice.

我想阅读解释为什么这是或不是一个好的做法。

采纳答案by Zachary Yates

Calling Thread.Abort()does raise an exception, and if you're writing code that will be re-used (or part of a base library) it's difficult for other developers to handle ThreadAbortExcpetions.

调用Thread.Abort()确实会引发异常,如果您正在编写将被重用的代码(或基础库的一部分),其他开发人员很难处理ThreadAbortExcpetions。

It's explained in this article about Reliability Best Practices.

在这篇关于可靠性最佳实践的文章中对此进行了解释。

I've always heard that calling Thread.Join()is a better way to do it, if you can wait until the thread is completed processing.

我一直听说调用Thread.Join()是一种更好的方法,如果您可以等到线程完成处理。

I don't know if anyone thinks it's a good practice. It can cause deadlocks (because unmanaged resources aren't properly cleaned up when you throw an exception)

我不知道是否有人认为这是一个好习惯。它可能导致死锁(因为在抛出异常时未正确清理非托管资源)

Here's another article about it, and other methods to deal with the issue.

这是关于它另一篇文章,以及处理该问题的其他方法。

回答by Yona

Interesting question. But I would advise against it since such a statement would prevent the method from being reused easily.

有趣的问题。但我建议不要这样做,因为这样的声明会阻止该方法被轻松重用。

回答by Brian Rasmussen

Calling Abort()on one's own thread is safe, but apart from that it should generally be avoided because you can't be sure other threads will terminate gracefully. In many cases you don't need to abort the thread. Just let it finish and it will be reclaimed.

调用Abort()自己的线程是安全的,但除此之外,通常应该避免它,因为您不能确定其他线程会正常终止。在许多情况下,您不需要中止线程。只要让它完成,它就会被收回。

回答by Juliet

Once the loop exits, the thread will terminate on its own. There is not need to abort the thread.

一旦循环退出,线程将自行终止。不需要中止线程。

The CurrentThread.Abortis not only superfluous, but genuinely harmful since it raises a ThreadAbortException. If another thread attempts to Join()your service loop thread, it will have to handle an exception unnecessarily. Your best bet is just to delete the line CurrentThread.Abort().

CurrentThread.Abort不仅是多余的,但真正有害的,因为它提出了一个ThreadAbortException。如果另一个线程尝试访问Join()您的服务循环线程,它将不得不处理一个不必要的异常。最好的办法就是删除该行CurrentThread.Abort()

回答by Paul Turner

A thread will naturally self-terminate when it has no further work to do: when the logic it was executing completes.

当一个线程没有进一步的工作要做时,它自然会自行终止:当它正在执行的逻辑完成时。

Thread.Abort()causes a ThreadAbortExceptionto be thrown on the current thread with the explicit purpose of rapidly terminating all execution on the thread. This is one of the special .NET exceptions which is "uncatchable": you can write a catch block but the exception will continue to be thrown after the catch block completes. This ensures there is no way an instruction to abort a thread can be stopped by subsequent user code.

Thread.Abort()导致ThreadAbortException在当前线程上抛出a ,其明确目的是快速终止线程上的所有执行。这是特殊的 .NET 异常之一,它是“无法捕获的”:您可以编写一个 catch 块,但在 catch 块完成后该异常将继续抛出。这确保了中止线程的指令无法被后续用户代码停止。

Calling Thread.Abort()is generally seen as bad practice as there are more graceful ways to terminate the logic you are executing. Cancellation is better handled using a CancellationToken.

调用Thread.Abort()通常被视为不好的做法,因为有更优雅的方式来终止您正在执行的逻辑。使用CancellationToken.