Newsgroups : Borland : borland.public.delphi.internet.winsock : 2006 Jun : Re: What is a socket?
| Subject: | Re: What is a socket? |
| Posted by: | "Remy Lebeau (TeamB)" (no.spam@no.spam.com) |
| Date: | Mon, 19 Jun 2006 14:53:08 |
"Richard Bibby" <richardjbibby@hotmail.com> wrote in message
news:44971bcc@newsgroups.borland.com...
> Why do you not recommend TTimer?
Because TService runs in a worker thread, and TTimer is not thread-safe
(like most other VCL components in general). If you place a TTimer on the
TService at design-time, the TTimer is going to be owned by, and run in the
context of, the main thread. Any acess to the TService object by the TTimer
OnTimer event handler will not be thread-safe by default, unless you are
implementing your own thread synchronization code.
> Would it be better to start a thread and use Sleep? The only
> problem then is I have to handle terminate requests in my thread.
You can have the TService.OnStart event begin the thread, and the
OnStop/Shutdown events terminate the thread.
Inside the thread itself, just use small Sleeps so that it can check the
Terminated property in a timely fashion. An alternative approach is to have
the thread create a TEvent object that the TService can signal when it wants
the thread to terminate. The thread can then wait on the event, with a
timeout, and terminate itself when the event is signalled.
Gambit