ThreadAbortException VS在C#优美事件句柄退出 [英] ThreadAbortException vs graceful event handle exit in C#

查看:159
本文介绍了ThreadAbortException VS在C#优美事件句柄退出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当中止线程的我总是一个体面退出之间,像这样的事件处理怀疑的执行:

When aborting the execution of a thread I'm always doubting between a graceful exit with an event handler like this:

int result = WaitHandle.WaitAny(handles);
if (result = WAIT_FINALIZE)
  FinalizeAndExit();

和使用事件信号的线程必须终止

and using the event to signal the thread it must terminate

或者只是处理ThreadAbortException来完成线程...

or just handling the ThreadAbortException to finalize the thread...

try
{
  // Main execution
}
catch(ThreadAbortException e)
{
  // FinalizeAndExit();
}
finally
{
}



我M通常倾向于使用ThreadAbortException方法,因为它可以处理,但它是在catch块的末尾加注,也避免了被保留的背信弃义的方法活着的线程,但我已经看到了这两种方法。 ..

I'm usually inclined to use the ThreadAbortException approach since it can be handled but it is reraised at the end of the catch block, and it also avoids the thread from being kept alive by "treacherous" methods, but I've seen both approaches...

你对此有何看法?有没有哪里是最好用一个比另一个任何情况下,它是最好使用总是接近X -

What's your opinion? Is there any situation where is best to use one over another, is it best to use always approach x?

推荐答案

一般情况下,第一种方法是preferrable。

Generally, the first method it preferrable.

这很难(如果不是不可能)编写的代码,将始终处理一个ThreadAbortException优雅。可发生于任何的线索恰好是做中间的异常,因此某些情况下是很难处理的。

It's hard (if not impossible) to write code that will always handle a ThreadAbortException gracefully. The exception can occur in the middle of whatever the thread happens to be doing, so some situations can be hard to handle.

例如,你能有后发生异常创建FileStream对象,但在此之前的基准分配给一个变量。这意味着你有一个应该处理的对象,但它的唯一基准丢失堆栈某处...

For example, the exception can occur after you have created a FileStream object, but before the reference is assigned to a variable. That means that you have an object that should be disposed, but the only reference to it is lost on the stack somewhere...

这篇关于ThreadAbortException VS在C#优美事件句柄退出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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