【问题标题】:How to continuously check for CPU usage in C#?如何在 C# 中持续检查 CPU 使用率?
【发布时间】:2014-08-13 18:58:19
【问题描述】:

我主要遵循该线程的第二个答案中讨论的内容。我想运行一个程序,它会在 10 秒内持续检查 CPU 使用率是否超过 5%,并在每次发生时提醒我。

How to get the CPU Usage in C#?

而我的代码如下:

static void Main(string[] args)
{
    Console.WriteLine("Checking for CPU usage");
    int totalhits = 0;
    float cpuPercent = getCPUValue();
    while (true)
    {
        if (cpuPercent >= 5)
        {
            totalhits += 1;
            if (totalhits == 10)
            {
                Console.WriteLine("Alert Usage has exceeded");
                Console.WriteLine("Press Enter to continue");
                Console.ReadLine();
                totalhits = 0;
            }
        }
        else
        {
            totalhits = 0;
        }
    }
}

private static float getCPUValue()
{
    PerformanceCounter cpuCounter = new PerformanceCounter();
    cpuCounter.CategoryName = "Processor";
    cpuCounter.CounterName = "% Processor time";
    cpuCounter.InstanceName = "_Total";

    float firstValue = cpuCounter.NextValue();
    System.Threading.Thread.Sleep(50);
    float secondValue = cpuCounter.NextValue();
    return secondValue;
}

我的问题是它永远不会达到那个阈值,如果我取出 totalhits = 0;最里面的 if 语句中的语句,那么它会在不到 5 秒的时间内达到阈值。

我做错了什么?

【问题讨论】:

  • float cpuPercent = getCPUValue();应该在你的循环内..
  • 您正在获取 CPU 百分比,然后在循环中旋转,永远检查。此外,每个循环迭代不是“1 秒”,您需要让线程休眠以模拟类似的东西。

标签: c# .net cpu-usage


【解决方案1】:

首先

float cpuPercent = getCPUValue();

行应该在循环内。否则,您只会读取一次 CPU 使用情况。并将迭代相同的值。

您应该只创建一个 PerformanceCounter 对象,然后在循环中一次又一次地调用 cpuCounter.NextValue()。 不要在每次迭代中创建相同的 CPU PerformanceCounter。

   PerformanceCounter counter = new PerformanceCounter("Processor", "% Processor Time", "_Total");
   while (true)
   {
       float cpuPercent = counter.nextValue();
       if (cpuPercent >= 5)
       {
           totalhits += 1;
           if (totalhits == 10)
           {
               Console.WriteLine("Alert Usage has exceeded");
               Console.WriteLine("Press Enter to continue");
               Console.ReadLine();
               totalhits = 0;
           }
       }
       else
       {
           totalhits = 0;
       }
   }

MSDN中所述

要获取需要初始值或先前值来执行必要计算的计数器的性能数据,请调用 NextValue 方法两次,并根据应用程序的需要使用返回的信息。

因此,您应该调用 cpuCounter.NextValue() 两次(两次调用之间有大约 1 秒的延迟)以开始获得正确的 CPU 使用结果。

顺便说一句,您应该在 CPU PerformanceCounter 的每次读取操作之间等待大约 1 秒(以确保更新)。

如本帖所示Retriving Accurate CPU Usate In C#

【讨论】:

  • 好的,我把它改成了你建议的,我的程序在 23 秒左右后达到了阈值,而不是 10 秒。在我按 Enter 再次执行后,它没有达到阈值。
  • @user3757114 我猜你的电脑真的很快。正如我在回答中添加的那样,您应该在每次迭代之间等待大约一秒钟。您是否考虑过您的 CPU 百分比在大部分时间都保持在 5% 以上的可能性?
  • 你太棒了!非常感谢!
  • 我应该在循环中调用这两个 .NextValue() 函数,还是一个在外部,然后另一个在内部?
  • @user3757114 可以通过两种方式完成。不过,我建议在循环之外使用 PerformanceCounter 进行初始化。您可以创建一个新方法,该方法将创建一个新的 PerformanceCounter,对其进行初始化,执行前两个 .NextValue() 调用,然后返回计数器。将返回的 PerformanceCounter 对象保存在循环之前的 cpuCounter 变量中,然后像往常一样在其中使用 .NextValue() 。
【解决方案2】:

使用下面 msdn 中所述的 DispatcherTimer 并根据需要获取结果。

DispatcherTimer

【讨论】:

  • 在 UI 线程上执行此操作是一个非常糟糕的主意。如果需要使用计时器,那么您可以使用 System.Threading.Timer 或 System.Timers.Timer 将其关闭
猜你喜欢
  • 2016-04-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-09-21
  • 1970-01-01
  • 2017-09-07
  • 2016-11-27
  • 2012-10-25
相关资源
最近更新 更多