Home

Windows Topics: The 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's 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 LearningPractical Learning: Counting the Computer's Ticks

  1. To start a new application, on the main menu, click File -> New VCL Forms Application - C++Builder
  2. Design the form as follows:
     
    Computer Ticks
    Control Alignment Caption Kind Name
    TGroupBox TImage   Elapsed Time    
    TLabel Label   Time this computer...   lblComputerTime
    TEdit Edit taRightJustify     edtComputerTime
    TLabel Label   millisconds    
    TLabel Label   Time since this application...   lblApplicationTime
    TEdit Edit taRightJustify     edtApplicationTime
    TLabel Label   millisconds    
    TTimer        
    TBitBtn Label     bkClose  
  3. Under the Code Editor, click the header tab to access the file
  4. In the private section of the form, declare an unsigned integer as follows:
    private:
    	unsigned int TimeTheComputerStarted; // User declarations
    public: // User declarations
    	__fastcall TForm1(TComponent* Owner);
    };
    //---------------------------------------------------------------------------
  5. Press F12 to display the form
  6. Double-click an unoccupied area of the form
  7. Implement the event as follows:
    //---------------------------------------------------------------------------
    void __fastcall TForm1::FormCreate(TObject *Sender)
    {
    	TimeTheComputerStarted = GetTickCount();
    }
    //---------------------------------------------------------------------------
  8. Press F12 to return to the form
  9. On the form, double-click the Timer1 icon to access its OnTimer event
  10. Implement the source file as follows:
    //---------------------------------------------------------------------------
    void __fastcall TForm1::Timer1Timer(TObject *Sender)
    {
    	unsigned long CurrentTickValue = GetTickCount();
    	unsigned int Difference = CurrentTickValue - TimeTheComputerStarted;
    
    	edtComputerTime->Text = UnicodeString(CurrentTickValue);
    	edtApplicationTime->Text = UnicodeString(Difference);
    }
    //---------------------------------------------------------------------------
  11. Press F9 to test the application
     
    Tick Counter
  12. After testing the application, close the form and return to your programming environment
  13. To make the values easier to read, change the form as follows: Delete both Edit boxes and both milliseconds labele:
     
    Tick Counter
  14. Press F12 to access the code
  15. Change the code of the OnTimer event as follows:
    //---------------------------------------------------------------------------
    void __fastcall TForm1::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;
    
    	UnicodeString ComputerTime, ApplicationTime;
    
    	ComputerTime = UnicodeString(ComputerHours) + L" hours, " +
    	UnicodeString(ComputerMinutes) + L" minutes, and " +
    	UnicodeString(ComputerSeconds) + L" seconds";
    	ApplicationTime = UnicodeString(ApplicationHours) + L" hours, " +
    	UnicodeString(ApplicationMinutes) + L" minutes, and " +
    	UnicodeString(ApplicationSeconds) + L" seconds";
    
    	lblComputerTime->Caption = L"This computer has been ON for "
    	 			   + ComputerTime;
    	lblApplicationTime->Caption = L"This application has been active for "
    				      + ApplicationTime;
    }
    //---------------------------------------------------------------------------
  16. Press F9 to test the application
     
    Tick Counter
  17. After testing the application, close the form and return to your programming environment
 
 
     
 

Home Copyright © 2010-2016, FunctionX