Tick Counter

Introduction

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 Microsoft Visual Basic and create a Standard EXE application
  2. Save the application in a new folder named Computer Time
  3. Save the form as frmMain and save the project as TickCounter
  4. Design the form as follows
     
    Control Properties
    Form Name: frmMain
    Border Style: 3 - FixedDialog
    Caption: Tick Counter
    Label Name: lblAppTime
    Label Name: txtCompTime
    Timer Interval: 20
  5. Double-click an unoccupied area on the form to launch its OnLoad event
  6. Also, initiate the OnTimer event of the Timer control
  7. Implement the file as follows:
     
    Private Declare Function Win32GetTickCounter Lib "Kernel32" Alias "GetTickCount" () As Long
    Private CompTime As Long
    Private Sub Form_Load()
        CompTime = Win32GetTickCounter
    End Sub
    
    Private Sub Timer1_Timer()
        Dim TickValue  As Long
        Dim Difference As Long
        
        TickValue = Win32GetTickCounter
        Difference = TickValue - CompTime
        
        Dim ComputerHours As Long
        Dim ComputerMinutes As Long
        Dim ComputerSeconds As Long
        Dim ApplicationHours, ApplicationMinutes, ApplicationSeconds As Long
    
        ComputerHours = (TickValue \ 3596400) Mod 24
        ComputerMinutes = (TickValue \ 59940) Mod 60
        ComputerSeconds = (TickValue \ 999) Mod 60
        ApplicationHours = (Difference \ 3596400) Mod 24
        ApplicationMinutes = (Difference \ 59940) Mod 60
        ApplicationSeconds = (Difference \ 999) Mod 60
        
        lblAppTime.Caption = "This application has been ON for " & _
                         CStr(ApplicationHours) & " hours, " & _
                         CStr(ApplicationMinutes) & " minutes, and " & _
                         CStr(ApplicationSeconds) & " seconds"
        
        lblCompTime.Caption = "This computer has been on for " & _
                         CStr(ComputerHours) & " hours, " & _
                         CStr(ComputerMinutes) & " mnutes, and " & _
                         CStr(ComputerSeconds) & " seconds"
    End Sub
  8. Test the application
 

Copyright © 2004-2014 FunctionX, Inc.