|
Windows Controls: The Tick Counter |
|
|
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:
private void Form1_Load(object sender, System.EventArgs e)
{
this.textBox1.Text = Environment.TickCount.ToString();
}
|
|
Application:
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
- Double-click an unoccupied area of the form to generate its Load
event
- Declare a global private integer named CompTime and initialize it in
the Load event as follows:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace CompAppElapsedTime1
{
public partial class Form1 : Form
{
private int CompTime;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
CompTime = Environment.TickCount;
}
}
}
- Return to the form
- Double-click the timer1 control and implement its Timer event as
follows:
private void timer1_Tick(object sender, EventArgs e)
{
int CurTickValue = Environment.TickCount;
int Difference = 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());
}
- Return to the form and double-click the Close button
- Implement the event as follows:
private void btnClose_Click(object sender, EventArgs e)
{
Close();
}
- 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 void timer1_Tick(object sender, EventArgs e)
{
int curTickValue = Environment.TickCount;
int difference = curTickValue - CompTime;
int computerHours, computerMinutes, computerSeconds;
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;
label1.Text = string.Format("This computer has been ON for {0} hours, {1} minutes {2} seconds",
computerHours.ToString(),
computerMinutes.ToString(),
computerSeconds.ToString());
label2.Text = string.Format("This application has been running for {0} hours, {1} minutes {2} seconds",
applicationHours.ToString(),
applicationMinutes.ToString(),
applicationSeconds.ToString());
}
- Execute the application to test it:
- After testing the application, close the form
|
|