Home

Topics on Windows Programming: Tick Counter

 

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.

Practical LearningPractical Learning: Counting the Computer's Ticks

  1. To start a new application, on the main menu, click File -> New Project...
  2. In the middle list, click MFC Application
  3. Set the name to TickCounter
  4. Click OK
  5. In the first page of the wizard, click Next
  6. In the second page of the wizard, click Dialog based
  7. Click Next
  8. In the third page of the wizard, click Finish
  9. Design the dialog box box as follows:
     
    Tick counter
    Control Caption ID
    Group Box Group Box Elapsed Time  
    Static Text Static Text This computer has been ON for XXXXXXXXXX IDC_COMPUTER_TIME
    Static Text Static Text This application has been running for XXXXXXXX IDC_APPLICATION_TIME
    Button Button Close IDCANCEL
  10. Right-click IDC_COMPUTER_TIME and click Add Variable...
  11. Set Category to Value and the Variable Type to CString
  12. Set the name to m_ComputerTime and click Finish
  13. Right-click IDC_APPLICATION_TIME and click Add Variable...
  14. Set the Category to Value and the Variable Type to CString
  15. Set the name to m_ApplicationTime and click Finish
  16. In the Solution Explorer, in the Header Files section, double-click TickCounterDlg
  17. Declare an unsigned integer as follows:
    public:
        CString m_ComputerTime;
        CString m_ApplicationTime;
        unsigned int ComputerTime;
    };
  18. In the Class View, click CTickCounterDlg and, in the lower part, double-click OnInitDialog
  19. Change the event as follows:
    BOOL CTickCounterDlg::OnInitDialog()
    {
        CDialogEx::OnInitDialog();
    
        // Add "About..." menu item to system menu.
    
        // IDM_ABOUTBOX must be in the system command range.
        ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
        ASSERT(IDM_ABOUTBOX < 0xF000);
    
        CMenu* pSysMenu = GetSystemMenu(FALSE);
        if (pSysMenu != NULL)
        {
    	BOOL bNameValid;
    	CString strAboutMenu;
    	bNameValid = strAboutMenu.LoadString(IDS_ABOUTBOX);
    	ASSERT(bNameValid);
    	if (!strAboutMenu.IsEmpty())
    	{
    		pSysMenu->AppendMenu(MF_SEPARATOR);
    		pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
    	}
        }
    
        // Set the icon for this dialog.  The framework does this automatically
        //  when the application's main window is not a dialog
        SetIcon(m_hIcon, TRUE);			// Set big icon
        SetIcon(m_hIcon, FALSE);		// Set small icon
    
        // TODO: Add extra initialization here
        ComputerTime = GetTickCount();
        SetTimer(1, 100, NULL);
    	
        return TRUE;  // return TRUE  unless you set the focus to a control
    }
  20. In the Class View, click CTickCounterDlg
  21. In the Properties window, click the Messages button Messages
  22. Click the WM_TIMER field and click the arrow of its box
  23. Select <Add> OnTimer and implement the event as follows:
  24.  implement it as follows:
    void CTickCounterDlg::OnTimer(UINT_PTR nIDEvent)
    {
        // TODO: Add your message handler code here and/or call default
        unsigned long CurTickValue = GetTickCount();
        unsigned int Difference = CurTickValue - ComputerTime;
    
        m_ComputerTime.Format(_T("This computer has been ON for %d"),
        				  CurTickValue);
        m_ApplicationTime.Format(_T("This application has been running for %d"),
        				   Difference);
    
        UpdateData(FALSE);
    
        CDialogEx::OnTimer(nIDEvent);
    }
  25. To test the application, press F5
     
    Tick counter
  26. After testing the application, close the dialog box and return to your programming environment
  27. To make the values easier to read, change the code of the OnTimer event as follows:
    void CTickCounterDlg::OnTimer(UINT_PTR nIDEvent)
    {
        // TODO: Add your message handler code here and/or call default
        unsigned long CurTickValue = GetTickCount();
        unsigned int Difference = CurTickValue - ComputerTime;
    
        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;
    
        m_ComputerTime.Format(_T("This computer has been ON for %d hours, %d minutes %d seconds"),
    		ComputerHours, ComputerMinutes, ComputerSeconds);
        m_ApplicationTime.Format(_T("This application has been running for %d hours, %d minutes %d seconds"),
    		ApplicationHours, ApplicationMinutes, ApplicationSeconds);
    
        UpdateData(FALSE);
        CDialogEx::OnTimer(nIDEvent);
    }
  28. To test the application, press F5
     
    Tick counter
     
    Tick counter
  29. After testing the application, close the dialog box and return to your programming environment
 
 
 
     
 

Home Copyright © 2010-2016, FunctionX