Thread.Abort()最后阻止 [英] Thread.Abort() and finally block

查看:114
本文介绍了Thread.Abort()最后阻止的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个childThread当前正在运行一个finally块来

清理外部资源。同时主线程调用

childThread.Abort()。问题是:当抛出ThreadAbortException

时,childThread是否完成了

finally块中的剩余代码?

Say I have a childThread currently is running a finally block to
cleanup external resources. At the same time the main thread calls
childThread.Abort(). The question is: when the ThreadAbortException
is thrown, does the childThread finish the remaining code in the
finally block?

推荐答案

" Bill" < FO ******* @ hotmail.com>在消息新闻中写道:bc ************************** @ posting.google.c om ...
"Bill" <fo*******@hotmail.com> wrote in message news:bc**************************@posting.google.c om...

说我有一个childThread当前正在运行一个finally块来清理外部资源。同时主线程调用了childThread.Abort()。问题是:当抛出ThreadAbortException
时,childThread是否完成了
finally块中的剩余代码?

Say I have a childThread currently is running a finally block to
cleanup external resources. At the same time the main thread calls
childThread.Abort(). The question is: when the ThreadAbortException
is thrown, does the childThread finish the remaining code in the
finally block?




进行实验。

这是找到这类问题答案的最佳方法。

编写一个简单的代码模型,看看它的作用。

即有一个线程在最后一个块中休息10秒,然后

然后打印一些消息。然后让主线程启动它,等待2秒

并中止它。看看邮件是否被打印出来。


离开我的头顶,没有编写和运行上面的例子,

我相信答案是不 ;。即,最后块将无法完成。


Ivan



Do the experiment.
This is the best way to find answer to such kind of questions.
Write a simple piece of code the models the situation and see what it does.
I.e. have a thread that sleeps for, say, 10 seconds in the final block and
then prints some message. Then have main thread start it, wait 2 seconds
and abort it. See if the message gets printed.

Off the top of my head, without writing and running the above example,
I believe the answer is "no". I.e., "finally" block will not be completed.

Ivan


保证finally块被调用。这条规则很少有例外。
。线程中止不是其中之一。

-

问候,

Alvin Bruney

得到DotNet ?在此处获取...
http:// www。 networkip.net/dotnet/tidbits/default.htm

" Ivan Krivyakov" <我*** @ verizon.net>在消息中写道

news:O0 ************* @ tk2msftngp13.phx.gbl ...
The finally block is guaranteed to be called. There are very few exceptions
to this rule. Thread aborts aren''t one of them.
--
Regards,
Alvin Bruney
Got DotNet? Get it here...
http://www.networkip.net/dotnet/tidbits/default.htm
"Ivan Krivyakov" <i.***@verizon.net> wrote in message
news:O0*************@tk2msftngp13.phx.gbl...
" Bill" < FO ******* @ hotmail.com>在留言中写道
news:bc ************************** @ posting.google.c om ...
"Bill" <fo*******@hotmail.com> wrote in message news:bc**************************@posting.google.c om...

说我有一个childThread当前正在运行一个finally块来清理外部资源。同时主线程调用了childThread.Abort()。问题是:当抛出ThreadAbortException
时,childThread是否完成了
finally块中的剩余代码?

Say I have a childThread currently is running a finally block to
cleanup external resources. At the same time the main thread calls
childThread.Abort(). The question is: when the ThreadAbortException
is thrown, does the childThread finish the remaining code in the
finally block?



进行实验。
这是找到这类问题答案的最佳方法。
在模型中编写一段简单的代码,看看



Do the experiment.
This is the best way to find answer to such kind of questions.
Write a simple piece of code the models the situation and see what it



的作用。即有一个线程在最后一个块中睡了10秒,然后打印一些消息。然后让主线程启动它,等待2秒
并中止它。看看消息是否被打印出来。

在我没有编写并运行上述示例的情况下,我不相信,答案是不。即,最后阻止将无法完成。

Ivan


does. I.e. have a thread that sleeps for, say, 10 seconds in the final block and
then prints some message. Then have main thread start it, wait 2 seconds
and abort it. See if the message gets printed.

Off the top of my head, without writing and running the above example,
I believe the answer is "no". I.e., "finally" block will not be completed.

Ivan



来自帮助文件:

"在线程中止之前执行Unexcucuted finally块;这个

包括抛出异常时执行的任何finally块。


-Rob Teixeira [MVP]


" Bill" < FO ******* @ hotmail.com>在消息中写道

news:bc ************************** @ posting.google.c om ...
From the help file:

"Unexecuted finally blocks are executed before the thread is aborted; this
includes any finally block that is executing when the exception is thrown."

-Rob Teixeira [MVP]

"Bill" <fo*******@hotmail.com> wrote in message
news:bc**************************@posting.google.c om...
说我有一个childThread当前正在运行一个finally块来清理外部资源。同时主线程调用了childThread.Abort()。问题是:当抛出ThreadAbortException
时,childThread是否完成了
finally块中的剩余代码?
Say I have a childThread currently is running a finally block to
cleanup external resources. At the same time the main thread calls
childThread.Abort(). The question is: when the ThreadAbortException
is thrown, does the childThread finish the remaining code in the
finally block?



这篇关于Thread.Abort()最后阻止的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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