Newsgroups : Borland : borland.public.delphi.internet.winsock : 2006 Apr : Re: TIdTCPClient.OnDisconnected not triggering

www.cryer.info
Managed Newsgroup Archive

Re: TIdTCPClient.OnDisconnected not triggering

Subject:Re: TIdTCPClient.OnDisconnected not triggering
Posted by:"Remy Lebeau (TeamB)" (no.spam@no.spam.com)
Date:Tue, 25 Apr 2006 14:58:52

"Bince" <foahchon@hotmail.com> wrote in message
news:444e9000$1@newsgroups.borland.com...

> the OnDisconnected event doesn't seem to be doing anything.

Indy is not an event-driven library.  OnDisconnect() is only triggered when
Disconnect() is called.  If the other party disconnects the socket, the
OnDisconnect event will NOT be triggered.  Instead, Indy will throw an
exception the next time any code tries to access the disconnected socket.

> I'm not sure whether the thread used for reading the received text is
being freed or not.

It is not.  Eventually, Execute() will exit, but the thread object remains
in memory until you explicitally Free() it.  Unless you set the
FreeOnTerminate property to true in the thread's constructor.

> Here's the relevant code:

The Execute() code is not thread-safe when updating the TMemo.  You must use
TThread.Synchronize() to access the GUI in a thread-safe manner.

>    rt.Terminate;
>    rt.WaitFor;

You need to disconnect the socket before calling WaitFor() or else the code
may deadlock.  When OnDisconnect is triggered, the socket is still active.
If the thread is blocked in a pending ReadLn(), your WaitFor() will never
exit because the thread is not able to terminate itself (unless new data
arrives, or you are using reading timeouts).

For a threaded client, I recommend deriving a new class from TIdTCPClient
and overriding the virtual Connect() and Disconnect() methods.  That allows
better management of a reading thread.


Gambit

Replies:

In response to:

www.cryer.info
Managed Newsgroup Archive