Delphi 2006 - What's the best way to gracefully kill a thread and still have the OnTerminate handler fire?

微笑、不失礼 提交于 2019-12-13 13:46:33

问题


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?


回答1:


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).

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.



来源:https://stackoverflow.com/questions/7863544/delphi-2006-whats-the-best-way-to-gracefully-kill-a-thread-and-still-have-the

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!