Delphi 2006-优雅地杀死线程并让OnTerminate处理程序启动的最佳方法是什么? [英] Delphi 2006 - What's the best way to gracefully kill a thread and still have the OnTerminate handler fire?

查看:189
本文介绍了Delphi 2006-优雅地杀死线程并让OnTerminate处理程序启动的最佳方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个线程有时会死死(我怀疑),这是由于DLL调用永不返回.在一般情况下,在您调用了Indy之类的阻塞例程的情况下,是否有一种方法可以从该方法中恢复,从而使线程OnTerminate处理程序触发?如果我调用TerminateThread,会发生这种情况吗?

I have a thread that sometimes freezes (I suspect) due to a DLL call that never returns. In the general case, where you have calls to blocking routines like Indy, is there a way of recovering from this in such a way that the thread OnTerminate handler fires? Will this happen if I call TerminateThread?

推荐答案

TerminateThread()是立即蛮力终止.它不会让OnTerminaate事件触发. OnTerminate可以触发的唯一方法是,如果线程的Execute()方法通过正常方式退出,无论是正常退出还是引发未捕获的异常(这将设置线程的FatalExpection属性).

TerminateThread() is an immediate brute-force termination. It will NOT let the OnTerminaate event fire. The only way OnTerminate can fire is if the thread's Execute() method exits through normal means, whether that be gracefully or by raising an uncaught exception (which will set the thread's FatalExpection property).

特别是在Indy的情况下,可以通过将套接字与另一个线程的上下文断开连接来中止套接字的阻塞操作.使用阻塞的DLL函数通常是不可能的,除非它们在其API中公开了这种功能.

In the case of Indy specifically, a blocking socket operation can be aborted by disconnecting the socket from the context of another thread. That is not usually possible with blocking DLL functions, unless they expose that kind of functionality in their API.

这篇关于Delphi 2006-优雅地杀死线程并让OnTerminate处理程序启动的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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