Home

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();
}
 

ApplicationApplication: 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. Double-click an unoccupied area of the form to generate its Load event
  6. 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;
            }
        }
    }
  7. Return to the form
  8. 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());
    }
  9. Return to the form and double-click the Close button
  10. Implement the event as follows:
     
    private void btnClose_Click(object sender, EventArgs e)
    {
            Close();
    }
  11. Test the application
     
    Tick Counter - Counting the elapsed time
  12. After testing the application, close it
  13. 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());
    }
  14. Execute the application to test it:
     
    Tick Counter - Easier to read
  15. After testing the application, close the form
 

Home Copyright © 2010-2014, FunctionX