How to Get a Performance Counter in C#?
namespace PerformanceCounters
{
class Program
{
static void Main(string[] args)
{
Timer _timer = new Timer(OnTick, null, 0, 2000);
Console.ReadLine();
}
public static TimeSpan UpTime
{
get
{
using (var uptime = new PerformanceCounter("System", "System Up Time"))
{
//Call this an extra time before reading its value
uptime.NextValue();
return TimeSpan.FromSeconds(uptime.NextValue());
}
}
}
private static void OnTick(object i_state)
{
Console.WriteLine("System Uptime:\t" + UpTime);
}
}
}