如何在关闭(X)的Onclose()事件中杀死一个线程 [英] How Do I Kill A Thread In Onclose() Event At Close(X)

查看:98
本文介绍了如何在关闭(X)的Onclose()事件中杀死一个线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在CLOSE(X)

how do i kill a thread in Onclose() event at CLOSE(X)

推荐答案

的Onclose()事件中杀死一个线程这是一个痛苦而复杂的问题,从而触发了激烈的讨论时间。很多人会告诉你,你永远不应该终止线程;并且一些线程应该优雅地终止自己(只是退出),在一些标志(在共享数据中)的一些轮询之后,它将给线程一个退出的命令。但我宁愿说永不说'永不';不幸的是,有很大的工程领域,这些东西是完全不可行的。我会说更多:操作系统的作者纯粹认识到存在这样的领域。



请看我过去的答案:

正确关闭dll中的线程 [ ^ ]。



即使你不会用它,它会让你知道这个看似简单的主题有多严肃。



因为我不知道你的申请,我可以先建议第一段所述的合作终止。如果它不适合你,我甚至可以提到非常脏的事情:调用 TerminateThread 。是的,它很脏并且强烈反对,但是如果你的线程的设计不允许简单的合作终止,你还有一个借口:如果这是你的应用程序做的最后一件事,你将不会有处理这次电话可能造成的不良后果。



-SA
PS :很有可能这篇文章将被投票。我建议你不要注意这点。这个主题通常知之甚少。
This is a painful and complicated issue, which triggers heated discussions from time to time. Many would tell you that you should never terminate thread; and a thread should gracefully terminate itself (just exit), after some polling of some flag (in shared data) which would give the thread an order to exit. But I rather say "never say 'never'"; unfortunately, there are whole big fields of engineering where such things are totally infeasible. I would say more: authors of OS purely recognize existence of such fields.

Please see my past answer:
Close correcly the thread inside a dll[^].

Even if you won't use it, it will give you the idea on how serious this seemingly simple topic is.

As I don't know your application, I can first advise the cooperative termination described in first paragraph. If it cannot work for you, I can even mention really dirty thing: a call to TerminateThread. Yes, it is dirty and strongly discouraged, but if the design of your thread does not allow simple cooperative termination you have one more excuse: if this is the last thing your application does, you won't have to deal with the possibly bad consequences of this call.

—SA
P.S.: It is very likely that this post will be down-voted. I would advise you to not pay attention for that. This topic is generally poorly understood.


这篇关于如何在关闭(X)的Onclose()事件中杀死一个线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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