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
Learning: Counting the Computer's Ticks
|
|
- Start a new Windows Application named CompAppElapsedTime1
- Design the form as follows:
- From the Components section of the Toolbox, click the Timer control
and click the form
- Change the timer's properties as follows:
Interval:20
Enabled: True
- Right-click the form and click View Code
- Declare an integer variable named CompTime
Public Class Form1
Private CompTime As Integer
End Class
|
- In the Class Name combo box, select (Form1 Events)
- 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
|
- In the Class Name combo box, select timer1
- 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
|
- In the Class Name combo box, select btnClose
- 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
|
- Test the application
- After testing the application, close it
- 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
|
- Execute the application to test it:
- After testing the application, close the form
|
|