Hey guys, I am here again with a problem.
In my program I need to calculate the current CPU usage.
I have searched google and found similar code across multiple websites but it just feels way too complicated for what I need to do AND my CPU never hits 100% usage with this calculation.
Not only does it look too complicated but i do not even understand the code, and when i write a program if i use code i find from google i try to understand it to become a better programmer.
So here is the code i found, if you know a better solution please help me out, and possibly try to explain it, and if there is not a better solution please help me understand this code.
Thank you all!
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.Threading;
using System.Diagnostics;
using ComTypes = System.Runtime.InteropServices.ComTypes;
namespace CPU_PI_Crusher
{
public class CpuUsage
{
[DllImport("kernel32.dll", SetLastError = true)]
static extern bool GetSystemTimes(
out ComTypes.FILETIME lpIdleTime,
out ComTypes.FILETIME lpKernelTime,
out ComTypes.FILETIME lpUserTime
);
ComTypes.FILETIME prevSysKernel;
ComTypes.FILETIME prevSysUser;
TimeSpan prevProcTotal;
DateTime lastRun;
Int16 int16CpuUsage;
long longRunCount;
public CpuUsage()
{
int16CpuUsage = -1;
lastRun = DateTime.MinValue;
prevSysUser.dwHighDateTime = prevSysUser.dwLowDateTime = 0;
prevSysKernel.dwHighDateTime = prevSysKernel.dwLowDateTime = 0;
prevProcTotal = TimeSpan.MinValue;
longRunCount = 0;
}
public short GetUsage()
{
short cpuCopy = int16CpuUsage;
if (Interlocked.Increment(ref longRunCount) == 1)
{
if (!EnoughTimePassed)
{
Interlocked.Decrement(ref longRunCount);
return cpuCopy;
}
ComTypes.FILETIME sysIdle, sysKernel, sysUser;
TimeSpan procTime;
Process process = Process.GetCurrentProcess();
procTime = process.TotalProcessorTime;
if (!GetSystemTimes(out sysIdle, out sysKernel, out sysUser))
{
Interlocked.Decrement(ref longRunCount);
return cpuCopy;
}
if (!IsFirstRun)
{
UInt64 sysKernelDiff = SubtractTimes(sysKernel, prevSysKernel);
UInt64 sysUserDiff = SubtractTimes(sysUser, prevSysUser);
UInt64 sysTotal = sysKernelDiff + sysUserDiff;
Int64 procTotal = procTime.Ticks - prevProcTotal.Ticks;
if (sysTotal > 0)
{
int16CpuUsage = (short)((100.0 * procTotal) / sysTotal);
}
}
prevProcTotal = procTime;
prevSysKernel = sysKernel;
prevSysUser = sysUser;
lastRun = DateTime.Now;
cpuCopy = int16CpuUsage;
}
Interlocked.Decrement(ref longRunCount);
return cpuCopy;
}
private UInt64 SubtractTimes(ComTypes.FILETIME a, ComTypes.FILETIME b)
{
UInt64 aInt = ((UInt64)(a.dwHighDateTime << 32)) | (UInt64)a.dwLowDateTime;
UInt64 bInt = ((UInt64)(b.dwHighDateTime << 32)) | (UInt64)b.dwLowDateTime;
return aInt - bInt;
}
private bool EnoughTimePassed
{
get
{
const int minimumElapsedMS = 250;
TimeSpan sinceLast = DateTime.Now - lastRun;
return sinceLast.TotalMilliseconds > minimumElapsedMS;
}
}
private bool IsFirstRun
{
get
{
return (lastRun == DateTime.MinValue);
}
}
}
}