65.9K
CodeProject is changing. Read more.
Home

Tiny UpTimeMeter

starIconstarIcon
emptyStarIcon
starIcon
emptyStarIconemptyStarIcon

2.12/5 (8 votes)

Jan 19, 2005

1 min read

viewsIcon

30550

downloadIcon

544

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.

#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:

       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.

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.