A Better Timer for a Delphi Programmer

The standard VCL TTimer component is a wrapper for the Windows API timer functions SetTimer and KillTimer. TTimer simplifies the processing of the WM_TIMER messages by converting them into OnTimer events. A timer based on WM_TIMER message processing cannot provide a resolution better than 10 milliseconds. And even when the timer interval is set to tens of milliseconds, the actual timer intervals are noticeably higher. The Windows API provides several opportunities to make a better timer – these are multimedia timers, waitable timers and queue timers. I have tried the third alternative (queue timer) as a replacement for the standard VCL TTimer component. The result is TksTimer component added to the ksTools package version 0.46. From the end user point of view the main difference between TTimer and TksTimer is a setting of the timer interval. TksTimer encapsulates the additional functionality of the queue timers and uses 2 properties (namely DueTime and Period) to set the timer interval. The DueTime property sets the interval between the moment the timer is enabled and the moment the timer fires for the first time. The Period property sets the interval between the subsequent timer events. If the Period is set to zero, the timer fires only once.
I have also written TimerTests application to compare the efficiency of TTimer and TksTimer. The difference is outstanding for the intervals of 100 milliseconds or less. I have obtained the following correlation between the set intervals and the average actual timer intervals (in milliseconds) on my computer (Pentium IV, 3.2 GHz, 2Gb RAM, Win XP):

Interval   TTimer  TksTimer
  100        108       100
   50         62        50
   20         31        20
   10         15        11
    5         15         6
    2         15         3
    1         14         2

6 thoughts on “A Better Timer for a Delphi Programmer

  1. If you change TTimer.Interval while TTimer is running the timer is stopped and restarted internally. It is easy to implement the same functionality for TksTimer, but I have not done it. Probably because TksTimer has two properties (DueTime and Period) instead of single TTimer.Interval.

    So you should explicitly stop and restart TksTimer to change interval value:

      Timer.Enabled:= False;
      Timer.DueTime:= ..;
      Timer.Period:= ..;
      Timer.Enabled:= True;
    • i have an while loop i will to slow his work to view there output step by step using a timer how i can do ?

  2. Serg
    It works good with Delphi 4 as long as I comment out Classes.

    FWindowHandle:= {Classes.}AllocateHWnd(WndProc)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s