Logo

The Tick Counter

Overview

The Win32 library 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 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.

  1. Start a Windows Forms Application named TickCounter
  2. Design the form as follows:
     
  3. Add a timer control to the form (it will be positioned in the lower section of the view). Set its Interval to 20 and its Enabled to True
  4. In the header file of the form, declare an unsigned integer as follows:
     
    #pragma once
    
    #include <windows.h>
    
    namespace TickCounter
    {
    	using namespace System;
    	using namespace System::ComponentModel;
    	using namespace System::Collections;
    	using namespace System::Windows::Forms;
    	using namespace System::Data;
    	using namespace System::Drawing;
    
    	/// <summary> 
    	/// Summary for Form1
    	///
    	/// WARNING: If you change the name of this class, you will need to change the 
    	///          'Resource File Name' property for the managed resource compiler tool 
    	///          associated with all .resx files this class depends on.  Otherwise,
    	///          the designers will not be able to interact properly with localized
    	///          resources associated with this form.
    	/// </summary>
    	public __gc class Form1 : public System::Windows::Forms::Form
    	{	
    	public:
    		Form1(void)
    		{
    			InitializeComponent();
    		}
    
    	private:
    		unsigned int CompTime;
      
    	. . .
                    
    	};
    }
    
  5. Double-click the form to get its Load event
  6. Initialize the CompTime variable as follows:
     
    private: System::Void Form1_Load(System::Object *  sender, System::EventArgs *  e)
    			 {
    				 CompTime = GetTickCount();
    			 }
  7. Double-click the timer1 control and implement its Timer event as follows:
     
    private: System::Void timer1_Tick(System::Object *  sender, System::EventArgs *  e)
    {
    	 unsigned long CurTickValue = GetTickCount();
    	 unsigned long Difference = CurTickValue - CompTime;
    				 
    	 label1->Text = String::Format(S"This computer has been ON for {0}", CurTickValue.ToString());
    	 label2->Text = String::Format(S"This application has been running for {0}", Difference.ToString());
    }
  8. Double-click the Close button and implement it as follows:
     
    private: System::Void btnClose_Click(System::Object *  sender, System::EventArgs *  e)
    {
    	 Close();
    }
  9. Test the application
     
  10. After testing the application, close it and return to MSVC
  11. To make the values easier to read, change the code of the OnTimer event as follows:
     
    private: System::Void timer1_Tick(System::Object *  sender, System::EventArgs *  e)
    {
    	 unsigned long CurTickValue = GetTickCount();
    	 unsigned long Difference = CurTickValue - CompTime;
    				 
    	 unsigned int ComputerHours, ComputerMinutes, ComputerSeconds;
    	 unsigned int ApplicationHours, ApplicationMinutes, ApplicationSeconds;
    
    	 ComputerHours = (CurTickValue / (3600 * 999)) % 24;
    	 ComputerMinutes = (CurTickValue / (60 * 999)) % 60;
    	 ComputerSeconds = (CurTickValue / 999) % 60;
    	 ApplicationHours = (Difference / (3600 * 999)) % 24;
    	 ApplicationMinutes = (Difference / (60 * 999)) % 60;
    	 ApplicationSeconds = (Difference / 999) % 60;
    
    	label1->Text = String::Format(S"This computer has been ON for {0} hours, {1} minutes {2} seconds",
    				  ComputerHours.ToString(),
    				  ComputerMinutes.ToString(),
    				  ComputerSeconds.ToString());
    
    	label2->Text = String::Format(S"This application has been running for {0} hours, {1} minutes {2} seconds",
    				  ApplicationHours.ToString(),
    				  ApplicationMinutes.ToString(),
    				  ApplicationSeconds.ToString());
     }
  12. Test the application:
     
  13. After testing the application, close it

 


Copyright © 2005 FunctionX, Inc.