What do you want to get from the instant use of the CPU (view) ...
In fact, instant CPU usage for the process does not exist. Instead, you need to take two measurements and calculate the average CPU usage, the formula is quite simple:
AvgCpuUsed = [TotalCPUTime (process, time2) - TotalCPUTime (process, time1)] / [time2-time1]
Time2 Time1, "" . Windows . , , , 5 , ...
, -,
using System.Diagnostics;
float GetAverageCPULoad(int procID, DateTme from, DateTime, to)
{
Process proc = Process.GetProcessById(procID);
System.TimeSpan lifeInterval = (to - from);
float CPULoad = (proc.TotalProcessorTime.TotalMilliseconds / lifeInterval.TotalMilliseconds) * 100;
return CPULoad / System.Environment.ProcessorCount;
}
"" :
class ProcLoad
{
public Dictionary<int, DateTime> lastCheckedDict = new Dictionary<int, DateTime>();
public float GetCPULoad(int procID)
{
if (lastCheckedDict.ContainsKey(procID))
{
DateTime last = lastCheckedDict[procID];
lastCheckedDict[procID] = DateTime.Now;
return GetAverageCPULoad(procID, last, lastCheckedDict[procID]);
}
else
{
lastCheckedDict.Add(procID, DateTime.Now);
return 0;
}
}
}
( , ) , , , Process.GetProcesses