我想在c#中获得一个应用程序的总的CPU使用情况。我已经找到了很多方法来深入研究进程的属性,但我只想知道进程的CPU使用情况,以及你在TaskManager中得到的总CPU。

我怎么做呢?


当前回答

没事的,我知道了!谢谢你的帮助!

下面是代码:

private void button1_Click(object sender, EventArgs e)
{
    selectedServer = "JS000943";
    listBox1.Items.Add(GetProcessorIdleTime(selectedServer).ToString());
}

private static int GetProcessorIdleTime(string selectedServer)
{
    try
    {
        var searcher = new
           ManagementObjectSearcher
             (@"\\"+ selectedServer +@"\root\CIMV2",
              "SELECT * FROM Win32_PerfFormattedData_PerfOS_Processor WHERE Name=\"_Total\"");

        ManagementObjectCollection collection = searcher.Get();
        ManagementObject queryObj = collection.Cast<ManagementObject>().First();

        return Convert.ToInt32(queryObj["PercentIdleTime"]);
    }
    catch (ManagementException e)
    {
        MessageBox.Show("An error occurred while querying for WMI data: " + e.Message);
    }
    return -1;
}

其他回答

这似乎对我有用,一个等待处理器达到一定百分比的例子

var cpuCounter = new PerformanceCounter("Processor", "% Processor Time", "_Total");
int usage = (int) cpuCounter.NextValue();
while (usage == 0 || usage > 80)
{
     Thread.Sleep(250);
     usage = (int)cpuCounter.NextValue();
}

比要求的多一点,但我使用额外的计时器代码来跟踪和警报,如果CPU使用率在持续1分钟或更长时间内达到90%或更高。

public class Form1
{

    int totalHits = 0;

    public object getCPUCounter()
    {

        PerformanceCounter cpuCounter = new PerformanceCounter();
        cpuCounter.CategoryName = "Processor";
        cpuCounter.CounterName = "% Processor Time";
        cpuCounter.InstanceName = "_Total";

                     // will always start at 0
        dynamic firstValue = cpuCounter.NextValue();
        System.Threading.Thread.Sleep(1000);
                    // now matches task manager reading
        dynamic secondValue = cpuCounter.NextValue();

        return secondValue;

    }


    private void Timer1_Tick(Object sender, EventArgs e)
    {
        int cpuPercent = (int)getCPUCounter();
        if (cpuPercent >= 90)
        {
            totalHits = totalHits + 1;
            if (totalHits == 60)
            {
                Interaction.MsgBox("ALERT 90% usage for 1 minute");
                totalHits = 0;
            }                        
        }
        else
        {
            totalHits = 0;
        }
        Label1.Text = cpuPercent + " % CPU";
        //Label2.Text = getRAMCounter() + " RAM Free";
        Label3.Text = totalHits + " seconds over 20% usage";
    }
}

我不喜欢在所有的PerformanceCounter解决方案中添加1秒的停顿。相反,我选择使用WMI解决方案。1秒等待/失速存在的原因是允许在使用PerformanceCounter时读取准确。然而,如果你经常调用这个方法并刷新这个信息,我建议不要经常产生这种延迟…即使考虑做一个异步进程来获得它。

我从这里的代码片段开始,使用c#返回WMI中的CPU使用情况,并在下面的博客文章中添加了解决方案的完整解释:

在c#中使用WMI获取所有核心的CPU使用情况

这个类每1秒自动轮询一次计数器,也是线程安全的:

public class ProcessorUsage
{
    const float sampleFrequencyMillis = 1000;

    protected object syncLock = new object();
    protected PerformanceCounter counter;
    protected float lastSample;
    protected DateTime lastSampleTime;

    /// <summary>
    /// 
    /// </summary>
    public ProcessorUsage()
    {
        this.counter = new PerformanceCounter("Processor", "% Processor Time", "_Total", true);
    }

    /// <summary>
    /// 
    /// </summary>
    /// <returns></returns>
    public float GetCurrentValue()
    {
        if ((DateTime.UtcNow - lastSampleTime).TotalMilliseconds > sampleFrequencyMillis)
        {
            lock (syncLock)
            {
                if ((DateTime.UtcNow - lastSampleTime).TotalMilliseconds > sampleFrequencyMillis)
                {
                    lastSample = counter.NextValue();
                    lastSampleTime = DateTime.UtcNow;
                }
            }
        }

        return lastSample;
    }
}

您可以使用WMI获取CPU百分比信息。如果您拥有正确的权限,您甚至可以登录到远程计算机。看看http://www.csharphelp.com/archives2/archive334.html,了解一下你能完成什么。

Win32_Process名称空间的MSDN引用可能也有帮助。

另见CodeProject示例如何:(几乎)一切在WMI通过c#。