Click here to Skip to main content
Click here to Skip to main content

Tiny UpTimeMeter

, 18 Jan 2005
Rate this:
Please Sign up or sign in to vote.
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.

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

Share

About the Author

Pumqara

United Kingdom United Kingdom
No Biography provided

Comments and Discussions

 
SuggestionAlternative PingroupPaul Pencoed22-Jul-11 6:24 
SuggestionAlternative PingroupPaul Pencoed22-Jul-11 6:23 
QuestionAlternative PingroupPaul Pencoed22-Jul-11 6:21 
Questioncan find category Pinmemberjma7826-Aug-07 21:32 
GeneralBad idea Pinmemberhk1118-Jul-07 20:49 
QuestionDoes not work in VC 2005 Pro. Why? Pinmemberjawsper3-Dec-06 1:00 
AnswerRe: Does not work in VC 2005 Pro. Why? Pinmemberyair_chen10-Aug-08 11:18 
GeneralPerformance Counter PinmemberJon Sagara19-Jan-05 1:47 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.141223.1 | Last Updated 19 Jan 2005
Article Copyright 2005 by Pumqara
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid