c# 获取 CPU 使用率高的进程

c# get process with high CPU usage

我正在寻找一种方法来获取消耗最高的进程名称 CPU。这是我的代码,它获取进程的 CPU 用法

static void Main(string[] args)
{
    PerformanceCounter myAppCpu = 
        new PerformanceCounter("Process", "% Processor Time", "OUTLOOK", true);

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

对于整个系统CPU,我有

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

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

有什么方法可以让我找到大多数 CPU 消耗进程?

The code gets a list of all runnig processes and assigns a PerformanceCounter to each of them. It then queries the counters with an intervall of 1000ms. Only the values with > 0% are outputted in descending order to the console.

using System;
using System.Linq;
using System.Threading;
using System.Diagnostics;
using System.Collections.Generic;

namespace ProcessCount
{
    static class Program
    {
        static void Main()
        {
            var counterList = new List<PerformanceCounter>();

            while (true)
            {
                var procDict = new Dictionary<string, float>();

                Process.GetProcesses().ToList().ForEach(p =>
                {
                    using (p)
                        if (counterList
                            .FirstOrDefault(c => c.InstanceName == p.ProcessName) == null)
                            counterList.Add(
                                new PerformanceCounter("Process", "% Processor Time",
                                    p.ProcessName, true));
                });

                counterList.ForEach(c =>
                {
                    try
                    {
                        // http://social.technet.microsoft.com/wiki/contents/
                        // articles/12984.understanding-processor-processor-
                        // time-and-process-processor-time.aspx

                        // This value is calculated over the base line of 
                        // (No of Logical CPUS * 100), So this is going to be a 
                        // calculated over a baseline of more than 100. 
                        var percent = c.NextValue() / Environment.ProcessorCount;
                        if (percent == 0)
                            return;

                        // Uncomment if you want to filter the "Idle" process
                        //if (c.InstanceName.Trim().ToLower() == "idle")
                        //    return;

                        procDict[c.InstanceName] = percent;
                    }
                    catch (InvalidOperationException) { /* some will fail */ }
                });

                Console.Clear();
                procDict.OrderByDescending(d => d.Value).ToList()
                    .ForEach(d => Console.WriteLine("{0:00.00}% - {1}", d.Value, d.Key));

                Thread.Sleep(1000);
            }
        }
    }
}