C# CPU使用率与任务管理器、PerformanceCounter或ManagementObjectSearcher不匹配

C# CPU使用率与任务管理器、PerformanceCounter或ManagementObjectSearcher不匹配,c#,cpu-usage,performancecounter,C#,Cpu Usage,Performancecounter,我只是想得到与任务管理器匹配的准确CPU使用率。到目前为止,我已经尝试了四种推荐的方法,但都不起作用 首先,我尝试了我能找到的类似解决方案或建议。这里的代码示例使用了四种方法,所有这些方法都不准确。其次,我知道任务管理器是波动的,它取决于采样的时间。这仍然不能解释这些差异。最后,我知道有不同的方法,任务管理器只使用一种方法。因为这是为一般用户设计的,所以它需要靠近任务管理器 public partial class MainWindow : Window { //*** Method 1

我只是想得到与任务管理器匹配的准确CPU使用率。到目前为止,我已经尝试了四种推荐的方法,但都不起作用

首先,我尝试了我能找到的类似解决方案或建议。这里的代码示例使用了四种方法,所有这些方法都不准确。其次,我知道任务管理器是波动的,它取决于采样的时间。这仍然不能解释这些差异。最后,我知道有不同的方法,任务管理器只使用一种方法。因为这是为一般用户设计的,所以它需要靠近任务管理器

public partial class MainWindow : Window
{
    //*** Method 1 & 2
    PerformanceCounter cpuCounterPi = new PerformanceCounter("Processor Information", "% Processor Time", "_Total");
    PerformanceCounter cpuCounterP = new PerformanceCounter("Processor", "% Processor Time", "_Total");

    //*** method 3
    ManagementObjectSearcher query1 = new ManagementObjectSearcher("select loadpercentage from win32_processor");

    //*** Mixed method usage below
    CounterSample csPi1, csPi2, csP1, csP2;
    double cpuPercentagePi, cpuPercentageP, cpuPercentageLoad;
    int count = -1;
    Boolean alternate = false;

    System.Windows.Threading.DispatcherTimer dispatcherTimer = new System.Windows.Threading.DispatcherTimer();

    public MainWindow()
    {
        InitializeComponent();

        dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick);
        dispatcherTimer.Interval = new TimeSpan(0, 0, 5);
        dispatcherTimer.Start();
    }

    private void dispatcherTimer_Tick(object sender, EventArgs e)
    {
        count++;
        //***Method 1 & 2
        if (alternate)
        {
            csPi1 = cpuCounterPi.NextSample();
            cpuPercentagePi = CounterSample.Calculate(csPi2, csPi1);
            csP1 = cpuCounterP.NextSample();
            cpuPercentageP = CounterSample.Calculate(csP2, csP1);

        }
        else
        {
            csPi2 = cpuCounterPi.NextSample();
            cpuPercentagePi = CounterSample.Calculate(csPi1, csPi2);
            csP2 = cpuCounterP.NextSample();
            cpuPercentageP = CounterSample.Calculate(csP1, csP2);
        }
        alternate = !alternate;

        if (count==5) { textBox.Clear(); count = 0; }
        textBox.Text = textBox.Text + "\nProcessor Information (Method 1)         " + cpuPercentagePi.ToString();
        textBox.Text = textBox.Text + "\nProcessor  (Method 2)                          " + cpuPercentageP.ToString();
        textBox.Text = textBox.Text + "\nProcessor ½  (Method 2 divided by 2)   " + (cpuPercentageP/2).ToString();
        //*****  Method 3 ****
        ManagementObjectCollection cpuColl = query1.Get();
        foreach (ManagementObject mo in cpuColl)
        {
            cpuPercentageLoad = Convert.ToDouble(mo["loadpercentage"]);
        }
        textBox.Text = textBox.Text +  "\nProcessor Load  (Method 3)                " + cpuPercentageLoad.ToString() + "\n";

    }
下面是两个与任务管理器进行比较的示例。

从图中你可以看到我有一个双核CPU,有四个逻辑处理器。CPU是运行Windows 10的Intel Skylake i7-6650U

谢谢

供参考-这些链接类似:
您有2个内核,但有4个线程,因此需要除以4,而不是2。另外,我怀疑您的WMI方法将只检查1个核心。检查实际上是否有多个
win32\u处理器
对象可用。

如果您注意到,一个方法确实被二除。如果我除以4,那么只有当它匹配到100%时,它会说是25%,并且会非常糟糕。所以,无论我怎么看,performanceCounter都远远不够。我将研究多个win32_处理器对象。我认为performanceCounter可以设置为读取每个核心,但据我所知,win32_处理器读取整个处理器。我看不到用loadpercentage读取单个内核的方法,因此它似乎应该读取整个CPU负载。。我当然不是专家。