D
Dirk Reske
Hello,
why doesn't this code work correctly?
private int GetCpuUsage(Process proc)
{
DateTime time1,time2;
TimeSpan timediff;
double cpu1,cpu2,cpudiff;
time1 = DateTime.Now;
cpu1 = proc.TotalProcessorTime.TotalMilliseconds;
Thread.Sleep(100);
proc.Refresh();
time2 = DateTime.Now;
cpu2 = proc.TotalProcessorTime.TotalMilliseconds;
cpudiff = cpu2 - cpu1;
timediff = time2 - time1;
return Convert.ToInt32(100 / timediff.TotalMilliseconds * cpudiff);
}
I get wrong values...perhaps the Idle process has an average cpu usage of
2950% ????
why doesn't this code work correctly?
private int GetCpuUsage(Process proc)
{
DateTime time1,time2;
TimeSpan timediff;
double cpu1,cpu2,cpudiff;
time1 = DateTime.Now;
cpu1 = proc.TotalProcessorTime.TotalMilliseconds;
Thread.Sleep(100);
proc.Refresh();
time2 = DateTime.Now;
cpu2 = proc.TotalProcessorTime.TotalMilliseconds;
cpudiff = cpu2 - cpu1;
timediff = time2 - time1;
return Convert.ToInt32(100 / timediff.TotalMilliseconds * cpudiff);
}
I get wrong values...perhaps the Idle process has an average cpu usage of
2950% ????