c# calculate CPU usage for a specific application

I'm trying to figure out how to get the CPU usage for a particular process but can only find information relating to overall CPU usage.

Does anyone know how to extract the current CPU usage in percentage terms for a specific application?


Performance Counters - Process - % Processor Time.

Little sample code to give you the idea:

using System;
using System.Diagnostics;
using System.Threading;

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

            Console.WriteLine("Press the any key to stop...\n");
            while (!Console.KeyAvailable)
            {
                double pct = myAppCpu.NextValue();
                Console.WriteLine("OUTLOOK'S CPU % = " + pct);
                Thread.Sleep(250);
            }
        }
    }
}

Notes for finding the instance based on Process ID:

I do not know of any better way, and hopefully somebody does. If not, here is one way you can find the right instance name for your process given the Process ID and process name.

There is another Performance Counter (PC) called "ID Process" under the "Process" family. It returns the PID for the instance. So, if you already know the name (i.e. "chrome" or "myapp"), you can then test each instance until you find the match for the PID.

The naming is simple for each instance: "myapp" "myapp#1" "myapp#2" ... etc.

...  new PerformanceCounter("Process", "ID Process", appName, true);

Once the PC's value equals the PID, you found the right appName. You can then use that appName for the other counters.


A method to calculate processor usage for a single process without using PerformanceCounter.

using System;
using System.Diagnostics;

namespace cpuusage
{
    class Program
    {
        private static DateTime lastTime;
        private static TimeSpan lastTotalProcessorTime;
        private static DateTime curTime;
        private static TimeSpan curTotalProcessorTime;

        static void Main(string[] args)
        {
            string processName = "OUTLOOK";

            Console.WriteLine("Press the any key to stop...\n");
            while (!Console.KeyAvailable)
            {
                Process[] pp = Process.GetProcessesByName(processName);
                if (pp.Length == 0)
                {
                    Console.WriteLine(processName + " does not exist");
                }
                else
                {
                    Process p = pp[0];
                    if (lastTime == null || lastTime == new DateTime())
                    {
                        lastTime = DateTime.Now;
                        lastTotalProcessorTime = p.TotalProcessorTime;
                    }
                    else
                    {
                        curTime = DateTime.Now;
                        curTotalProcessorTime = p.TotalProcessorTime;

                        double CPUUsage = (curTotalProcessorTime.TotalMilliseconds - lastTotalProcessorTime.TotalMilliseconds) / curTime.Subtract(lastTime).TotalMilliseconds / Convert.ToDouble(Environment.ProcessorCount);
                        Console.WriteLine("{0} CPU: {1:0.0}%",processName,CPUUsage * 100);

                        lastTime = curTime;
                        lastTotalProcessorTime = curTotalProcessorTime;
                    }
                }

                Thread.Sleep(250);
            }
        }
    }
}

You could loop through the processes to pick which one, or if you already know the id, simply use this command instead of GetProcessesByName()

Process p = Process.GetProcessById(123);