Click here to Skip to main content
15,885,278 members
Articles / Desktop Programming / Windows Forms
Article

Tiny UpTimeMeter

Rate me:
Please Sign up or sign in to vote.
2.12/5 (8 votes)
18 Jan 20051 min read 30K   544   12   8
A small class to measure the uptime of your PC without the 24,9 days limit.

Sample Image - UpTime.jpg

Introduction

UpTimeMeter is nothing but a tiny sample showing how to measure the uptime of your PC in seconds, minutes, hours and days. It will solve the problem with the 24,9 days limit of the System.Environment.TickCount property. The demo project is coded using Microsoft Visual C# Express 2005 Beta 1.

Background

I use an Int64 value and copy the current TickCount there. This extends the limit pretty much. I create a Timer, so I could update the Int64 value myself every 1 second. The function GetUptime() fills a TimeStruct structure with the proper time.

UpTimeMeter

Since the code is pretty elementary, I do not think it needs much explanation. Here is the UpTimeMeter. Reading the comments in the code will be enough to understand the code.

C#
#region Using directives

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Timers;

#endregion

namespace UpTimeMeter 
{
    //
    // In such structure will the GetUptime() store the proper time
    //
    public struct TimeStruct
    {
        public int Seconds;
        public int Minutes;
        public int Hours;
        public Int64 Days;
    }

    public class UpTime: IDisposable
    {
        Int64 uptime;   // Here will the UpTime value be stored
        Timer meter;    // The Timer

        //
        // This function will create a Timer and will freeze it.
        //
        public UpTime()
        {
                // new Timer with Interval = 1 second
            meter = new Timer(1000);
            meter.Stop();
        }

        //
        // This function will calculate how much days, hours,
        // minutes, seconds have passed.
        // Requires a ref to a TimeStruct as parameter.
        //
        public void GetUptime(ref TimeStruct time)
        {
            time.Seconds = (int)uptime % 60;

            if (uptime >= 60)
            {
                time.Minutes = (int)(uptime / 60) % 60;

                if (uptime >= 3600)
                {
                    time.Hours = (int)((uptime / 60) / 60) % 24;

                    if (uptime >= (3600 * 24))
                    {
                        time.Days = (uptime / 3600) / 24;
                    }
                }
            }
        }

        //
        // This function will start the UpTimeMeter.
        //
        public void Start()
        {
            // Converts milliseconds to seconds
            // and stores them in the Int64 uptime
            uptime = System.Environment.TElapsedEventHandlerickCount / 1000;
            // Creates an ElapsedEventHandler,
            // which will update the uptime every 1 sec.
            meter.Elapsed += new ElapsedEventHandler(Counter);
            // Starts the Timer
            meter.Start();
        }

        //
        // This function will stop the UpTimeMeter.
        //
        public void Stop()
        {
                // Stops the Timer
            meter.Stop();
        }

        //
        // This is the function to update the uptime every second using the Timer,
        // created in the construtor.
        //
        void Counter(object sender, ElapsedEventArgs e)
        {
                // 1 second passed -> increase uptime
            uptime++;

        }

        #region IDisposable Members

        public void Dispose()
        {
            meter.Dispose();
        }

        #endregion
    }
}

Probably the part which needs explanation is the GetUptime() function:

C#
public void GetUptime(ref TimeStruct time)
 {
     time.Seconds = (int)uptime % 60;

     if (uptime >= 60)
     {
         time.Minutes = (int)(uptime / 60) % 60;

         if (uptime >= 3600)
         {
             time.Hours = (int)((uptime / 60) / 60) % 24;

             if (uptime >= (3600 * 24))
             {
                 time.Days = (uptime / 3600) / 24;
             }
         }
     }
 }

The algorithm is pretty simple. Every minute has 60 seconds, every hour - 60 minutes, and every day - 24 hours. 125 seconds / 60 = 2 minutes and 5 seconds. Same is for the hours, and for the days we divide by 24 (24 hours in one day). Of course, 45 minutes is less than an hour, so that's why I check it before I make the division.

Demo Project

It represents a WinForm with a Label and a Timer. The Timer calls every 1 sec the GetUptime() function and converts the data from the TimeStruct to a whole string to represent the uptime.

C#
namespace UpTimeTestApp
{
    partial class Form1 : Form
    {
        UpTime test;
        TimeStruct timez;

        public Form1()
        {
            test = new UpTime();
            timez = new TimeStruct();

            InitializeComponent();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            test.Start();
            test.GetUptime(ref timez);
            label1.Text = timez.Days.ToString() + " day(s) "
                            + timez.Hours.ToString() + " h "
                            + timez.Minutes.ToString() + " min "
                            + timez.Seconds.ToString() + " sec";
        }
    }
}

Points of Interest

I coded this while testing the Microsoft Visual C# 2005 Express Beta 1 and the .NET Framework 2.0 Beta 1. The code is nothing special - hopefully it will be useful to someone.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here



Comments and Discussions

 
SuggestionAlternative Pin
Paul_Williams22-Jul-11 5:24
Paul_Williams22-Jul-11 5:24 
SuggestionAlternative Pin
Paul_Williams22-Jul-11 5:23
Paul_Williams22-Jul-11 5:23 
QuestionAlternative Pin
Paul_Williams22-Jul-11 5:21
Paul_Williams22-Jul-11 5:21 
Questioncan find category Pin
jma7826-Aug-07 20:32
jma7826-Aug-07 20:32 
GeneralBad idea Pin
Harkamal Singh18-Jul-07 19:49
Harkamal Singh18-Jul-07 19:49 
QuestionDoes not work in VC 2005 Pro. Why? Pin
jawsper3-Dec-06 0:00
jawsper3-Dec-06 0:00 
AnswerRe: Does not work in VC 2005 Pro. Why? Pin
yair_chen10-Aug-08 10:18
yair_chen10-Aug-08 10:18 
GeneralPerformance Counter Pin
Jon Sagara19-Jan-05 0:47
Jon Sagara19-Jan-05 0:47 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.