Newsgroups : Borland : borland.public.delphi.internet.winsock : 2008 Feb : Re: OnWork does not fire in Indy10 Delphi 2007
| Subject: | Re: OnWork does not fire in Indy10 Delphi 2007 |
| Posted by: | "Remy Lebeau (TeamB)" (no.spam@no.spam.com) |
| Date: | Tue, 12 Feb 2008 10:48:55 |
"Moore" <moore@hotmail.com> wrote in message
news:47b19cd7@newsgroups.borland.com...
> in a TIdTCPClient OnWork event i have the following code :
You should NOT be reading or writing anything inside the OnWork events.
They are triggered inside reading/writing operations that are already in
progress. What you are doing is a good way to corrupt your communication
with the server, or crash/deadlock your code.
> it seems the OnWork does not fire as I see no text in Edit1
It is not supposed to, as Indy is not an event-driven library to begin with
(with few exceptions). OnWork does not tell you when new data has arrived.
Instead, it reports progress information for a reading/writing operation
that you tell Indy to perform.
> am I doing it the right way?
No.
> or there is a better way to make the OnWork event fire?
You should not be relying on the OnWork event like that to begin with. Indy
is a blocking library. Just call ReadLn() after Connect() exits, and let
Indy wait for the data to arrive. If you are calling ReadLn() in the
context of the main thread, then you will have to place a TIdAntiFreeze
component onto the form to prevent the UI from freezing up. Or
alternatively set the ReadTimeout property to a small value and then look at
the ReadLnTimedOut property whenever ReadLn() exits.
If you wait an asynchronous event to occur when new data arrives, then you
will have to either:
1) use a Timer that periodically calls the IOHandler's
CheckForDataOnSource() method, and then query the InputBuffer.Size property
to see if any data has been buffered for use.
2) move your reading code into its own worker thread so that Indy can block
normally without blocking the main thread. Your worker thread will have to
communicate with the main thread if it needs to make any updates after
processing the data.
Gambit