Search code examples
multithreadingdelphidelphi-2009tthreadomnithreadlibrary

How to implement thread which periodically checks something using minimal resources?


I would like to have a thread running in background which will check connection to some server with given time interval. For example for every 5 seconds.

I don't know if there is a good "desing pattern" for this? If I remember corretly, I've read somewehere that sleeping thread in its execute method is not good. But I might be wrong.

Also, I could use normal TThread class or OTL threading library.

Any ideas?

Thanks.


Solution

  • You could use an event and implement the Execute method of the TThread descendant by a loop with WaitForSingleObject waiting for the event, specifying the timeout. That way you can wake the thread up immediately when needed, e.g. when terminating.