2

This is the new class with the method to get the cpu usage:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using OpenHardwareMonitor.Hardware;
using System.Diagnostics;
using DannyGeneral;
using System.Windows.Forms;
using System.Threading;
using System.Management;
using System.Globalization;
using System.IO;
using System.Runtime.InteropServices;

namespace HardwareMonitoring
{


    class CpuUsages
    {
        public static string processes;

        public static string cputest()
        {
            PerformanceCounter cpuCounter = new PerformanceCounter();
            cpuCounter.CategoryName = "Processor";
            cpuCounter.CounterName = "% Processor Time";
            cpuCounter.InstanceName = "_Total";
            var unused = cpuCounter.NextValue(); 
            System.Threading.Thread.Sleep(1000); 
            processes = "Cpu usage: " + cpuCounter.NextValue() + "%";
            return processes;
        }
    }
}

This is in form1 i have a timer tick event set to 1000ms interval:

private void timer3_Tick(object sender, EventArgs e)
        {            
                CpuUsages.cputest();
                cpuusage = CpuUsages.processes;
                label26.Text = cpuusage;
        }

I see in label26 each a second an updated value of the CPU usage but it's not the same as in the windows task manager. When in the task manager I see for example 34% overall CPU usage in my program I see 21% or 27% but it's never the same.

What's wrong with the calculation maybe ?

Here is a screenshot marked with circles:

enter image description here

user3681442
  • 297
  • 3
  • 15

0 Answers0