简体   繁体   English

CPU 使用率与 Task Manager、PerformanceCounter 或 ManagementObjectSearcher 不匹配

[英]CPU usage does not match Task Manager, PerformanceCounter or ManagementObjectSearcher

I am just trying to get accurate CPU usage that matches Task Manager.我只是想获得与任务管理器匹配的准确 CPU 使用率。 So far I have tried four recommended methods that do not work.到目前为止,我已经尝试了四种不起作用的推荐方法。

First, I have tried the similar solutions or suggestions given that I could find.首先,鉴于我能找到,我尝试了类似的解决方案或建议。 The code sample here uses four methods, all of which are inaccurate.这里的代码示例使用了四种方法,都是不准确的。 Second, I know that Task Manager fluctuates and depends on when it is sampled.其次,我知道任务管理器会波动并取决于采样时间。 That still does not account for the differences.这仍然不能解释差异。 Lastly, I know there are different methods, and that Task Manager uses just one method.最后,我知道有不同的方法,而任务管理器只使用一种方法。 Since this is intended for general users, it needs to be close to Task Manager.由于这是面向一般用户的,因此需要靠近任务管理器。

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";

    }

Here are two samples comparing to task manager.这是与任务管理器相比的两个示例。 在此处输入图片说明 在此处输入图片说明

From the picture you can see I have a dual core CPU, with four logical processors.从图中可以看出我有一个双核 CPU,有四个逻辑处理器。 The CPU is a Intel Skylake i7-6650U, running Windows 10. CPU 是 Intel Skylake i7-6650U,运行 Windows 10。

Thanks谢谢

For reference - These links are similar: PerformanceCounter reporting higher CPU usage than what's observed How to get the CPU Usage in C#?供参考 - 这些链接是类似的: PerformanceCounter 报告的 CPU 使用率比观察到的要高How to get the CPU Usage in C#? Calculate CPU usage for a Windows process? 计算 Windows 进程的 CPU 使用率?

You have 2 cores but 4 threads, so you need to divide by 4, not 2. Also your WMI method I suspect will only be checking 1 core.您有 2 个内核但有 4 个线程,因此您需要除以 4,而不是 2。此外,我怀疑您的 WMI 方法只会检查 1 个内核。 Check if there are in fact multiple win32_processor objects available.检查实际上是否有多个win32_processor对象可用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM