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
{
public struct TimeStruct
{
public int Seconds;
public int Minutes;
public int Hours;
public Int64 Days;
}
public class UpTime: IDisposable
{
Int64 uptime;
Timer meter;
public UpTime()
{
meter = new Timer(1000);
meter.Stop();
}
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;
}
}
}
}
public void Start()
{
uptime = System.Environment.TElapsedEventHandlerickCount / 1000;
meter.Elapsed += new ElapsedEventHandler(Counter);
meter.Start();
}
public void Stop()
{
meter.Stop();
}
void Counter(object sender, ElapsedEventArgs e)
{
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.