I want to retrieve the cpu usage of my system using c#. I have read that I must call the method NextValue()
then wait for about a second and call the NextValue()
method again.
I want to do that asynchronous to not delay my UI thread so I have written following code
public Task<float> GetCpuPerformanceAsync()
{
return Task.Run(() =>
{
CpuPerformance.NextValue();
Task.Delay(1000);
return CpuPerformance.NextValue();
});
}
This is the declaration of CpuPerformance
CpuPerformance = new PerformanceCounter("Processor Information", "% Processor Time", "_Total");
At the first time calling the asynchronous method as shown above it returns me the actual cpu usage but then calling it again after few secons it only shows 0 or 100 which doesn't coincide with the usage shown in my task manager
Can somebody help me solving this please?