FunctionX Practical Learning Logo

Tick Counter

Introduction

The Win32 API provides a special function used to count a specific number of lapses that have occurred since you started your computer. This information or counter is available through the GetTickCount() function. Its syntax is:

function GetTickCount: DWORD; stdcall;

This function takes no argument. If it succeeds in performing its operation, which it usually does, it provides the number of milliseconds that have elapsed since you started your computer. Just like the VCL Timer control, what you do with the result of this function is up to you and it can be used in various circumstances. For example, computer games and simulations make great use of this function.

After retrieving the value that this function provides, you can display it in a text-based control. Here is an example:

procedure TForm1.Button1Click(Sender: TObject);
var
   Elapsed: Integer;

begin
    Elapsed := GetTickCount();
    edtElapsed.Text := IntToStr(Elapsed);
end;

Practical Learning: Counting the Computer's Ticks

  1. Start Borland Delphi and create a new application with its default form
  2. Save it in a new folder named CompTicks1
  3. Save the unit as Exercise and save the project CompTicks
     
  4. Design the form as follows: set its BorderStyle to bsDialog
  5. Change its name to frmMain and set its Caption to Counting Computer Ticks
  6. Add a GroupBox control and set its Caption to Elapsed Time
  7. Add a Timer control from the System tab of the Component Palette
  8. Set its Interval to 20
  9. Add an Edit control and change its Name to edtComputerTime
  10. Add another Edit control and change its Name to edtApplicationTime
  11. Press F12 to access the Code Editor. In the declaration section, declare an integer named TimeTheComputerStarted 
  12. On the form, double-click the Timer1 icon to access its OnTimer event and implement the file as follows:
     
    unit Unit1;
    
    interface
    
    uses
      Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
      StdCtrls, ExtCtrls, Buttons;
    
    type
      TfrmMain = class(TForm)
        GroupBox1: TGroupBox;
        BitBtn1: TBitBtn;
        Timer1: TTimer;
        Label1: TLabel;
        Label2: TLabel;
        edtComputerTime: TEdit;
        edtApplicationTime: TEdit;
        Label3: TLabel;
        Label4: TLabel;
        procedure FormCreate(Sender: TObject);
        procedure Timer1Timer(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
      end;
    
    var
      frmMain: TfrmMain;
      TimeTheComputerStarted: DWORD;
    
    implementation
    
    {$R *.DFM}
    
    procedure TfrmMain.FormCreate(Sender: TObject);
    begin
         TimeTheComputerStarted := GetTickCount();
    end;
    
    procedure TfrmMain.Timer1Timer(Sender: TObject);
    var
        CurrentTickValue: DWORD;
        Difference:       DWORD;
    begin
        CurrentTickValue := GetTickCount();
    	Difference       := CurrentTickValue - TimeTheComputerStarted;
    
    	edtComputerTime.Text    := IntToStr(CurrentTickValue);
    	edtApplicationTime.Text := IntToStr(Difference);
    end;
    
    procedure TfrmMain.BitBtn1Click(Sender: TObject);
    begin
         Close
    end;
    
    end.
  13. Press F9 to test the application
     
  14. After testing the application, close it and return to Delphi
  15. To make the values easier to read, change the form as follows. Delete both Edit boxes and replace them with Label controls named lblComputerTime and lblApplicationTime respectively:
     
  16. Change the code of the OnTimer event as follows:
     
    procedure TfrmMain.Timer1Timer(Sender: TObject);
    var
        CurrentTickValue: DWORD;
        Difference:       DWORD;
        ComputerHours, ComputerMinutes, ComputerSeconds: DWORD;
        ApplicationHours, ApplicationMinutes, ApplicationSeconds: DWORD;
        ComputerTime, ApplicationTime: AnsiString;
    begin
        CurrentTickValue := GetTickCount();
    	Difference       := CurrentTickValue - TimeTheComputerStarted;
    
    	ComputerHours      := (CurrentTickValue div (3600 * 999)) mod 24;
    	ComputerMinutes    := (CurrentTickValue div (60 * 999)) mod 60;
    	ComputerSeconds    := (CurrentTickValue div 999) mod 60;
    	ApplicationHours   := (Difference div (3600 * 999)) mod 24;
    	ApplicationMinutes := (Difference div (60 * 999)) mod 60;
    	ApplicationSeconds := (Difference div 999) mod 60;
    
    	ComputerTime := IntToStr(ComputerHours) + ' hours, ' +
    	IntToStr(ComputerMinutes) + ' minutes ' +
    	IntToStr(ComputerSeconds) + ' seconds';
    	ApplicationTime := IntToStr(ApplicationHours) + ' hours ' +
    	IntToStr(ApplicationMinutes) + ' minutes ' +
    	IntToStr(ApplicationSeconds) + ' seconds';
    
    	lblComputerTime.Caption    := ComputerTime;
    	lblApplicationTime.Caption := ApplicationTime;
    end;
  17. Test the application
     
  18. After testing the application, close it
  19. Save All

 

 

Home Copyright © 2004 FunctionX, Inc. VCL