0

The question is about HTTP get implemented in a thread using IdHTTP. I want to terminate the thread gracefully if needed. For this purpose I check the Terminated property periodically within the HTTPWork event of IdHTTP. If the the terminate property is set then IdHTTP.disconnect method is called and the thread terminates.

This works fine, however consider the following problematic scenario:

For some reason the network goes down when the thread is started. After a timeout there is a socket error exception which is also OK, however during the timeout interval there are no events fired from IdHTTP, and therefore there is no way to check for the terminated property. So the question is how to gracefully terminate a thread which uses IdHTTP during a connection timeout?

4

1 回答 1

2

最优雅的方法是简单地让线程超时,不要试图强迫它。但是如果你必须强制它,你可以TIdHTTP.Disconnect()从另一个线程上下文中调用,例如试图终止你的工作线程的那个。任何后续的套接字操作,或任何仍在进行中的操作,都将立即失败,并在工作线程的上下文中出现套接字错误。

于 2014-09-21T20:20:00.143 回答