Logo

The Tick Counter

The .Net provides a special property used to count a specific number of lapses that have occurred since you started your computer. This information or counter is available through the Environment.TickCount property.

This property provides the number of milliseconds that have elapsed since you started your computer. Just like the timer control, what you do with the result 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 property provides, you can display it in a text-based control.

Overview

  1. Start Microsoft Visual Basic .Net and create a Windows 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. Declare a global private integer named CompTime
     
    Public Class Form1
        Inherits System.Windows.Forms.Form
    
        Private CompTime As System.Int32
    
    End Class
  5. Double-click the form to get its Load event
  6. Initialize the CompTime variable as follows:
     
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            CompTime = Environment.TickCount
        End Sub
  7. Double-click the timer1 control and implement its Timer event as follows:
     
    Private Sub timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles timer1.Tick
            Dim CurTickValue As System.Int32 = Environment.TickCount
            Dim Difference As System.Int32 = CurTickValue - CompTime
    
            label1.Text = String.Format("This computer has been ON for {0}", CurTickValue.ToString())
            label2.Text = String.Format("This application has been running for {0}", Difference.ToString())
        End Sub
  8. Double-click the Close button and implement it as follows:
     
    Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
            Close()
        End Sub
  9. Test the application
     
  10. After testing the application, close it
  11. To make the values easier to read, change the code of the OnTimer event as follows:
     
    Private Sub timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles timer1.Tick
            Dim CurTickValue As System.Int32 = Environment.TickCount
            Dim Difference As System.Int32 = CurTickValue - CompTime
    
            Dim ComputerHours As System.Int32
            Dim ComputerMinutes As System.Int32
            Dim ComputerSeconds As System.Int32
            Dim ApplicationHours As System.Int32
            Dim ApplicationMinutes As System.Int32
            Dim ApplicationSeconds As System.Int32
    
            ComputerHours = (CurTickValue / (3600 * 999)) Mod 24
            ComputerMinutes = (CurTickValue / (60 * 999)) Mod 60
            ComputerSeconds = (CurTickValue / 999) Mod 60
    
            ApplicationHours = (Difference / (3600 * 999)) Mod 24
            ApplicationMinutes = (Difference / (60 * 999)) Mod 60
            ApplicationSeconds = (Difference / 999) Mod 60
    
            label1.Text = String.Format("This computer has been ON for {0} hours, {1} minutes {2} seconds", _
                               CStr(ComputerHours), _
                               CStr(ComputerMinutes), _
                               CStr(ComputerSeconds))
    
            label2.Text = String.Format("This application has been running for {0} hours, {1} minutes {2} seconds", _
                                    CStr(ApplicationHours), _
                                    CStr(ApplicationMinutes), _
                                    CStr(ApplicationSeconds))
        End Sub
  12. Test the application:
     
  13. After testing the application, close it
 

Copyright © 2004-2014 FunctionX, Inc.