简体   繁体   English

广泛使用ThreadAbortException

[英]Extensive use of ThreadAbortException

I'm working in a legacy project that has this exception handling code in many methods. 我正在一个旧项目中,该项目在许多方法中都具有此异常处理代码。

catch(ThreadAbortException e)
{
  ...
}

I don't see anywhere in the project Thread.Abort() or Thread.Interrupt() calls. 我在项目Thread.Abort()或Thread.Interrupt()调用中看不到任何地方。 Is it safe to delete all these ThreadAbortException handling or it is some other way that can be raised. 删除所有这些ThreadAbortException处理是否安全,还是可以采用其他方法。

Official docs: "The exception that is thrown when a call is made to the Abort method." 官方文档:“调用Abort方法时引发的异常。” If you are completely sure there are no calls to Thread.Abort then you might as well erase those catch blocks. 如果您完全确定没有对Thread.Abort的调用,则最好擦除那些catch块。

EDIT: Be mindful that your code may be running in the context of an external app that may call Thread.Abort on your threads. 编辑:请注意,您的代码可能在可能调用Thread.Abort的外部应用程序的上下文中运行。

Not that it matters anyway as a ThreadAbortException can't really be handled as the CLR itself will rethrow it to actually kill the thread ASAP. 但这并不重要,因为ThreadAbortException不能真正被处理,因为CLR本身会重新抛出它以尽快杀死线程。

"Actually yes, a ThreadAbortException is special. Even if you handle it, it will be automatically re-thrown by the CLR at the end of the try/catch/finally. (As noted in the comments, it can be suppressed with ResetAbort but by that point the code smells like rotten fish.)" - Read this question for more details: ThreadAbortException “实际上是的,ThreadAbortException是特殊的。即使您处理它,它也会在try / catch / finally结束时由CLR自动重新抛出。(如注释中所述,可以使用ResetAbort将其抑制,但是到那时,代码闻起来像烂鱼。)”-请阅读此问题以获取更多详细信息: ThreadAbortException

Well if answering specifically your question I would say that it would be better not to delete these exception handlers as it's most likely that they were added by some developer trying to solve a problem. 好吧,如果专门回答您的问题,我会说最好不要删除这些异常处理程序,因为它们很可能是某些试图解决问题的开发人员添加的。 And I think there was a reason to add those handlers so if you just remove this code it can lead to appearing of some bugs again in the future. 而且我认为添加这些处理程序是有原因的,因此,如果您仅删除此代码,将来可能会再次出现一些错误。

Regarding the ThreadAbordException : I know for sure that it can be throwed not only with calling Thread.Abort() method when you are debugging (it might be a bug in VS, I'm not sure) and it forces your program to just crash silently. 关于ThreadAbordException :我可以确定,不仅可以在调试时通过调用Thread.Abort()方法来抛出它(它可能是VS中的错误,我不确定),并且它会迫使程序崩溃默默。 So depending on what's inside of those handlers it could be possible that a developer was trying to solve such problem. 因此,根据这些处理程序内部的内容,开发人员可能试图解决此类问题。

Also remember that you could be invoking methods of third-party libraries, web-services etc. in a separate thread, too. 还请记住,您也可能在单独的线程中调用第三方库,Web服务等的方法。 I'm not sure if they can throw such an exception but that's a possible case to consider. 我不确定他们是否可以抛出这样的异常,但这是一个可能的情况。

Is the project running on a main thread and spinning up background worker threads? 项目是否在主线程上运行并启动后台工作线程? If the main thread exits while background threads are running, a ThreadAbortedException can occur on the background threads. 如果主线程在后台线程运行时退出,则可能在后台线程上发生ThreadAbortedException。

The catch statement could specifically handle this scenario, where no error actually occurred on the background thread, in a different manner than any other exception. catch语句可以专门处理这种情况,即在后台线程上实际上没有发生错误,其方式与其他异常不同。

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

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