Windows Controls: 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:
private void Form1_Load(object sender, System.EventArgs e)
{
this.textBox1.Text = Environment.TickCount.ToString();
}
Practical Learning: Counting the Computer's Ticks
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; } } }
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()); }
private void btnClose_Click(object sender, EventArgs e) { Close(); }
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()); }
|
||
Home | Copyright © 2010-2020, FunctionX | |
|