为什么线程已中止出现异常消息? [英] Why thread has been aborted exception message occur?

查看:127
本文介绍了为什么线程已中止出现异常消息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我中止该异常时,该异常已终止
有什么办法可以消除这种例外....



[没有大写字母的相同问题]

当我中止线程时,发生异常,线程已被中止
有什么办法可以消除此异常........

WHEN I ABORT THE THREAD AN EXCEPTION OCCURS THAT THREAD HAS BEEN ABORTED
IS THERE ANY WAY TO REMOVE THIS EXCEPTION....



[Same Question without all the capitals]

When I abort the thread an exception occurs that thread has been aborted
Is there any way to remove this exception........

推荐答案

不是我所发现的,我只是抓住了它并继续前进.我相信这样做是为了使在该线程中运行的代码有一种方法可以知道其停止.这样一来,您可以捕获异常并在线程消失之前进行所需的任何清理(我认为Final仍将运行).

自.NET 1.1以来,我还没有真正搞过Threads或winForms,所以可能有所改变.如果我错了,请随时纠正我.
Not that i''ve ever found, i simply catch it and move on. I believe it was done so that the code running in that thread has a way to know its stopping. That way you can catch the exception and do whatever clean up you need (i think Finally''s will still run) before the thread goes away.

I havent really messed with Threads or winForms since .NET 1.1, so something may have changed. Feel free to correct me if i''m wrong.


不. ThreadAbortException的目的是通知异常终止的线程,该时间是它自己清除之后的时间,并释放它可能持有的任何资源.如果线程没有机会执行此操作,则可能会使应用程序处于不一致状态-意味着从资源泄漏到损坏的内存都可能发生,并且很可能在以后导致应用程序崩溃. >
由于这个原因,中止线程被认为是不好的做法.最好使用一种协作方法来通知线程退出时间,而不是使用Thread.Abort(),以便线程可以照顾干净地退出以保持程序的一致性.
No. The purpose of the ThreadAbortException is to notify aborted threads that it''s time to cleanup after itself and release any resources that it might be holding. If a thread doesn''t have an opportunity to do this it could leave the application in an inconsistent - that means anything from leaking resources to corrupted memory - and that will most likely cause an application crash later on down the line.

Aborting threads is considered bad practice for this reason. Instead of using Thread.Abort(), it''s better to work out a cooperative method of notifying threads that it''s time to exit so that they can take care of exiting cleanly to maintain the consistency of the program.


SomeGuyThatIsMe是正确的.正如您可以阅读有关 Thread.Abort方法(System.Threading)的 MSDN 文档一样. ) [ ^ ]:

在线程上调用此方法时,系统会在线程中抛出ThreadAbortException以中止它.ThreadAbortException是一种特殊的异常,可由应用程序代码捕获,但在捕获结束时会重新抛出除非调用ResetAbort,否则块.ResetAbort取消中止请求,并防止ThreadAbortException终止线程.未执行的finally块在线程中止之前执行."

这将使线程有机会清理它可能已分配的资源,并拒绝中止请求.

如果不需要任何清理,只需将线程方法包装在try/catch块中.
SomeGuyThatIsMe is right. As you can read on the MSDN documentation about Thread.Abort Method (System.Threading)[^]:

"When this method is invoked on a thread, the system throws a ThreadAbortException in the thread to abort it. ThreadAbortException is a special exception that can be caught by application code, but is re-thrown at the end of the catch block unless ResetAbort is called. ResetAbort cancels the request to abort, and prevents the ThreadAbortException from terminating the thread. Unexecuted finally blocks are executed before the thread is aborted."

This will give the thread a chance to cleanup the resources that it could have allocated, and to reject the request to abort.

If you don''t need any cleanup, simply wrap your thread method inside a try/catch block.


这篇关于为什么线程已中止出现异常消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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