Newsgroups : Borland : borland.public.delphi.internet.winsock : 2007 Mar : Re: Indy10 - TIdTCPConnection, Open/Close, ReadLnTimedOut, Read Thread
| Subject: | Re: Indy10 - TIdTCPConnection, Open/Close, ReadLnTimedOut, Read Thread |
| Posted by: | "Remy Lebeau (TeamB)" (no.spam@no.spam.com) |
| Date: | Fri, 30 Mar 2007 10:06:18 |
"Michael Stieler" <michael.stieler@rie.eu> wrote in message
news:460d1981@newsgroups.borland.com...
> It now works better but I got another problem back I hoped to have
eliminated:
Which is what exactly?
> After disconnection I terminate the thread with .Terminate
Are you calling WaitFor(), though? You should be.
> and Free it in the OnTerminate event handler.
Do not do that! That is not a valid place to free the thread. That
is a good way to crash the VCL and deadlock other threads. Just free
the thread after WaitFor() has exited instead, ie:
constructor TMyComp.Create(AOwner: TComponent);
begin
conn := TIdTCPClient.Create(Self);
end;
procedure TMyComp.Connect;
begin
conn.Host := ...
conn.Port := ...
RIELog('Connecting');
conn.Connect;
try
readThread := TReadThread.Create(conn);
readThread.OnRead := Read;
readThread.Resume;
except
conn.Disconnect;
raise;
end;
RIELog('Connected');
end;
procedure TMyComp.Disconnect;
begin
RIELog('Disconnecting');
if readThread <> nil then readThread.Terminate;
try
conn.Disconnect;
finally
if readThread <> nil then
begin
readThread.WaitFor;
FreeAndNil(readThread);
end;
end;
RIELog('Disconnected');
end;
Gambit