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:

DWORD GetTickCount(VOID);

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:

//---------------------------------------------------------------------------
void __fastcall TForm1::btnElapsedClick(TObject *Sender)
{
	unsigned long Elapsed = GetTickCount();
	edtElapsed->Text = IntToStr(Elapsed);
}
//---------------------------------------------------------------------------

Practical Learning: Counting the Computer's Ticks

  1. Start Borland C++ Builder 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. Change its name to frmMain and set its Caption to Counting Computer Ticks
  5. Add a GroupBox control and set its Caption to Elapsed Time
  6. Add a Timer control from the System tab of the Component Palette. Set its Interval to 20
  7. Add an Edit control and change its Name to edtComputerTime
  8. Add another Edit control and change its Name to edtApplicationTime
  9. Press F12 to access the Code Editor. In the private section of the form, declare an unsigned integer as follows:
     
    private:
        unsigned int TimeTheComputerStarted; // User declarations
    public: // User declarations
        __fastcall TfrmMain(TComponent* Owner);
    };
    //---------------------------------------------------------------------------
  10. 10. On the form, double-click the Timer1 icon to access its OnTimer event and implement the source file as follows:
     
    //---------------------------------------------------------------------------
    
    #include <vcl.h>
    #pragma hdrstop
    
    #include "Unit1.h"
    //---------------------------------------------------------------------------
    #pragma package(smart_init)
    #pragma resource "*.dfm"
    TfrmMain *frmMain;
    //---------------------------------------------------------------------------
    __fastcall TfrmMain::TfrmMain(TComponent* Owner)
    : TForm(Owner)
    {
    	TimeTheComputerStarted = GetTickCount();
    }
    //---------------------------------------------------------------------------
    void __fastcall TfrmMain::Timer1Timer(TObject *Sender)
    {
    	unsigned long CurrentTickValue = GetTickCount();
    	unsigned int Difference = CurrentTickValue - TimeTheComputerStarted;
    
    	edtComputerTime->Text = IntToStr(CurrentTickValue);
    	edtApplicationTime->Text = IntToStr(Difference);
    }
    //---------------------------------------------------------------------------
    void __fastcall TfrmMain::BitBtn1Click(TObject *Sender)
    {
    	Close();
    }
    //---------------------------------------------------------------------------
  11. Press F9 to test the application
     
  12. After testing the application, close it and return to Bcb
  13. 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:
     
  14. Change the code of the OnTimer event as follows:
     
    //---------------------------------------------------------------------------
    void __fastcall TfrmMain::Timer1Timer(TObject *Sender)
    {
    	unsigned long CurrentTickValue = GetTickCount();
    	unsigned int Difference = CurrentTickValue - TimeTheComputerStarted;
    
    	unsigned int ComputerHours, ComputerMinutes, ComputerSeconds;
    	unsigned int ApplicationHours,
    			ApplicationMinutes, ApplicationSeconds;
    
    	ComputerHours = (CurrentTickValue / (3600 * 999)) % 24;
    	ComputerMinutes = (CurrentTickValue / (60 * 999)) % 60;
    	ComputerSeconds = (CurrentTickValue / 999) % 60;
    	ApplicationHours = (Difference / (3600 * 999)) % 24;
    	ApplicationMinutes = (Difference / (60 * 999)) % 60;
    	ApplicationSeconds = (Difference / 999) % 60;
    
    	AnsiString ComputerTime, ApplicationTime;
    
    	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;
    }
    //---------------------------------------------------------------------------
  15. Test the application
     
  16. After testing the application, close it
  17. Save All
 

Home Copyright © 2004-2007 FunctionX, Inc. VCL