I have a h: TIdHTTP component in a secondary thread and it’s retrieving headers from a URL. At the same time user clicks cancel and the main thread tries to actually disconnect the Indy component. Is it safe to call thrd.h.Disconnect from the main thread? (while h:TIdHTTP is declared as public so is accessible within the main thread)
If it is safe at anytime, may I write a Cancel function in the thread that can be called directly by the main thread?
Thanks in advance
Yes, the main thread can call
Disconnect()on the socket while the worker thread is busy reading/writing data over the same socket. The read/write operation will raise an exception that the thread can catch or ignore as needed.You could also assign an event handler to the
TIdHTTP.OnWorkevent and have it raise an exception, such as withSysUtils.Abort(), if the thread is being terminated.