In Delphi I have a threaded client that listens for response in a thread. Thread is declared like:
TMyThread = class(TThread)
private
FClient: TIdTCPClient;
FStringResponse: string;
protected
procedure Execute; override;
procedure DoSynch;
public
constructor Create(AClient: TIdTCPClient);
end;
I connect using the:
if not IdTCPClient1.Connected then
begin
// // Set the Host
IdTCPClient1.Host := Edit1.Text;
// // Set the port
IdTCPClient1.Port := 65535;
// // Connect
IdTCPClient1.Connect;
try
MyThread := TMyThread.Create(IdTCPClient1);
except
IdTCPClient1.Disconnect;
raise;
end;
end;
I try to disconnect using:
if MyThread <> nil then
begin
MyThread.Terminate;
// MyThread.WaitFor;
MyThread.Free;
MyThread := nil;
IdTCPClient1.Disconnect;
end;
But this disconnect code throws an exception. What's the proper way to do terminate this thread and to disconnect the client?