Home

Topics: The Tick Counter

 

Description

The Environment class 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 TickCount property. This property counts 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 property is up to you and it can be used in various circumstances.

     

After retrieving the value that the Environment.TickCount property, you can display it in a text-based control. Here is an example:

Imports System.Drawing
Imports System.Windows.Forms

Module Exercise

    Public Class Starter
        Inherits Form

        Dim components As System.ComponentModel.Container

        Public Sub New()
            InitializeComponent()
        End Sub

        Public Sub InitializeComponent()

        End Sub

        Private Sub FormLoaded(ByVal sender As Object, _
            ByVal e As EventArgs) Handles Me.Load
            Text = CStr(Environment.TickCount)
        End Sub
    End Class

    Function Main() As Integer

        Dim frmStart As Starter = New Starter

        Application.Run(frmStart)

        Return 0
    End Function

End Module 

Practical LearningPractical Learning: Counting the Computer's Ticks

  1. Start a new Windows Application named CompAppElapsedTime1
  2. Design the form as follows:
     
    Tick Counter - Form Design
  3. From the Components section of the Toolbox, click the Timer control Timer and click the form
  4. Change the timer's properties as follows:
    Interval:20
    Enabled: True
  5. Right-click the form and click View Code
  6. Declare an integer variable named CompTime
     
    Public Class Form1
        Private CompTime As Integer
    End Class
  7. In the Class Name combo box, select (Form1 Events)
  8. In the Method Name combo box, select Load and initialize the variable in the Load event as follows:
     
    Private Sub Form1_Load(ByVal sender As Object, _
                               ByVal e As System.EventArgs) _
                               Handles Me.Load
            CompTime = Environment.TickCount
    End Sub
  9. In the Class Name combo box, select timer1
  10. In the Method Name combo box, select Tick and implement the event as follows:
     
    Private Sub timer1_Tick(ByVal sender As Object, _
                                ByVal e As System.EventArgs) _
                                Handles timer1.Tick
        Dim CurTickValue As Integer = Environment.TickCount
        Dim Difference As Integer = CurTickValue - CompTime
    
        label1.Text = "This computer has been ON for " & CStr(CurTickValue)
        label2.Text = "This application has been running for " & CStr(Difference)
    End Sub
  11. In the Class Name combo box, select btnClose
  12. In the Method Name combo box, select Click and implement the event as follows:
     
    Private Sub btnClose_Click(ByVal sender As Object, _
                                   ByVal e As System.EventArgs) _
                                   Handles btnClose.Click
            End
    End Sub
  13. Test the application
     
    Tick Counter - Counting the elapsed time
  14. After testing the application, close it
  15. To make the values easier to read, change the code of the OnTimer event as follows:
     
    Private Sub timer1_Tick(ByVal sender As Object, _
                                ByVal e As System.EventArgs) _
                                Handles timer1.Tick
            Dim CurrentTickValue As Integer
            Dim Difference As Integer
    
            Dim ComputerHours As Integer
            Dim ComputerMinutes As Integer
            Dim ComputerSeconds As Integer
            Dim ApplicationHours As Integer
            Dim ApplicationMinutes As Integer
            Dim ApplicationSeconds As Integer
    
            CurrentTickValue = Environment.TickCount
            Difference = CurrentTickValue - CompTime
            ComputerHours = (CurrentTickValue / (3600 * 999)) Mod 24
            ComputerMinutes = (CurrentTickValue / (60 * 999)) Mod 60
            ComputerSeconds = (CurrentTickValue / 999) Mod 60
    
            ApplicationHours = (Difference / (3600 * 999)) Mod 24
            ApplicationMinutes = (Difference / (60 * 999)) Mod 60
            ApplicationSeconds = (Difference / 999) Mod 60
    
            label1.Text = "This computer has been ON for " & _
                          CStr(ComputerHours) & _
                          " hours, " & _
                          CStr(ComputerMinutes) & _
                          " minutes, " & _
                          CStr(ComputerSeconds) & _
                          " seconds"
    
            label2.Text = "This application has been running for " & _
                          CStr(ApplicationHours) & _
                          "  hours, " & _
                          CStr(ApplicationMinutes) & _
                          "  minutes " & _
                          CStr(ApplicationSeconds) & _
                          "  seconds"
    End Sub
  16. Execute the application to test it:
     
    Tick Counter - Easier to read
  17. After testing the application, close the form

 

 

Home Copyright © 2008-2016, FunctionX, Inc.