[Lazarus] TTimer woes (newbie)

Michael Schnell mschnell at lumino.de
Tue Apr 1 10:44:01 CEST 2014


On 03/31/2014 07:51 PM, Flávio Etrusco wrote:
>
> TTimer in Windows (and the basic timer support in Windows ) is 
> implemented using window messages and you're not allowing the app to 
> process messages inside your loop.

This necessity is not limited to Windows nor to TTimer.


Any Object Pascal Event handler (hence most code done in "normal" 
projects) is driven by the Project's Event Queue (which in Windows in 
turn is driven by Windows messages).

In fact using "Application.ProcessMessages" in a loop is a workaround 
that is necessary when you really need a long winding loop in one of the 
(main Thread-) - Events) calculating something.

It is a lot better style to do short events, whenever possible (maybe 
doing the long winding stuff in a TThread). Infinite (polling) loops in 
fact are (close to) forbidden, as they eat up CPU cycles that can better 
be used for other activities in the project or in any other program 
running on that PC.

In fact Bob is not doing this (eating up CPU Cycles), but he uses 
wait(), which only hampers it's own project. This also usually is (close 
to) forbidden in the main thread, as the GUI will stop working while the 
main thread waits.

Thus he should either use a TTimer instead of waiting and continue the 
work to be done in a newly entered Timer Event, or execute the code that 
includes the wait() in a TThread.

-Michael




More information about the Lazarus mailing list