Calculating process cpu usage from Process.TotalProcessorTime
Building upon 111WARLOCK111's answer, you can use the following minimal code to measure the current CPU load:
async Task<double> GetCpuLoadAsync(TimeSpan MeasurementWindow)
{
Process CurrentProcess = Process.GetCurrentProcess();
TimeSpan StartCpuTime = CurrentProcess.TotalProcessorTime;
Stopwatch Timer = Stopwatch.StartNew();
await Task.Delay(MeasurementWindow);
TimeSpan EndCpuTime = CurrentProcess.TotalProcessorTime;
Timer.Stop();
return (EndCpuTime - StartCpuTime).TotalMilliseconds / (Environment.ProcessorCount * Timer.ElapsedMilliseconds);
}
Use the following code to call the function and replace the TimeSpan with the window to monitor:
double CpuLoad = await GetCpuLoadAsync(TimeSpan.FromSeconds(10));
class GetCPUUsage
{
static TimeSpan start;
public static double CPUUsageTotal
{
get;
private set;
}
public static double CPUUsageLastMinute
{
get;
private set;
}
static TimeSpan oldCPUTime = new TimeSpan(0);
static DateTime lastMonitorTime = DateTime.UtcNow;
public static DateTime StartTime = DateTime.UtcNow;
// Call it once everything is ready
static void OnStartup()
{
start = Process.GetCurrentProcess().TotalProcessorTime;
}
// Call this every 30 seconds
static void CallCPU()
{
TimeSpan newCPUTime = Process.GetCurrentProcess().TotalProcessorTime - start;
CPUUsageLastMinute = (newCPUTime - oldCPUTime).TotalSeconds / (Environment.ProcessorCount * DateTime.UtcNow.Subtract(lastMonitorTime).TotalSeconds);
lastMonitorTime = DateTime.UtcNow;
CPUUsageTotal = newCPUTime.TotalSeconds / (Environment.ProcessorCount * DateTime.UtcNow.Subtract(StartTime).TotalSeconds);
oldCPUTime = newCPUTime;
}
}
class GetCPUInfo
{
public static string GetInfoMinute()
{
return String.Format("{0:0.0}", GetCPUUsage.CPUUsageLastMinute * 100);
}
public static string GetInfoTotal()
{
return String.Format("{0:0.0}", GetCPUUsage.CPUUsageTotal * 100);
}
}