How to get the CPU Usage in C#?

C#Cpu Usage

C# Problem Overview


I want to get the overall total CPU usage for an application in C#. I've found many ways to dig into the properties of processes, but I only want the CPU usage of the processes, and the total CPU like you get in the TaskManager.

How do I do that?

C# Solutions


Solution 1 - C#

You can use the PerformanceCounter class from System.Diagnostics.

Initialize like this:

PerformanceCounter cpuCounter;
PerformanceCounter ramCounter;

cpuCounter = new PerformanceCounter("Processor", "% Processor Time", "_Total");
ramCounter = new PerformanceCounter("Memory", "Available MBytes");

Consume like this:

public string getCurrentCpuUsage(){
            return cpuCounter.NextValue()+"%";
}

public string getAvailableRAM(){
            return ramCounter.NextValue()+"MB";
} 

Solution 2 - C#

A little more than was requsted but I use the extra timer code to track and alert if CPU usage is 90% or higher for a sustained period of 1 minute or longer.

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

Solution 3 - C#

After spending some time reading over a couple different threads that seemed pretty complicated I came up with this. I needed it for an 8 core machine where I wanted to monitor SQL server. For the code below then I passed in "sqlservr" as appName.

private static void RunTest(string appName)
{
    bool done = false;
    PerformanceCounter total_cpu = new PerformanceCounter("Process", "% Processor Time", "_Total");
    PerformanceCounter process_cpu = new PerformanceCounter("Process", "% Processor Time", appName);
    while (!done)
    {
        float t = total_cpu.NextValue();
        float p = process_cpu.NextValue();
        Console.WriteLine(String.Format("_Total = {0}  App = {1} {2}%\n", t, p, p / t * 100));
        System.Threading.Thread.Sleep(1000);
    }
}

It seems to correctly measure the % of CPU being used by SQL on my 8 core server.

Solution 4 - C#

It's OK, I got it! Thanks for your help!

Here is the code to do it:

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

Solution 5 - C#

You can use WMI to get CPU percentage information. You can even log into a remote computer if you have the correct permissions. Look at http://www.csharphelp.com/archives2/archive334.html to get an idea of what you can accomplish.

Also helpful might be the MSDN reference for the Win32_Process namespace.

See also a CodeProject example How To: (Almost) Everything In WMI via C#.

Solution 6 - C#

CMS has it right, but also if you use the server explorer in visual studio and play around with the performance counter tab then you can figure out how to get lots of useful metrics.

Solution 7 - C#

This class automatically polls the counter every 1 seconds and is also thread safe:

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

Solution 8 - C#

This seems to work for me, an example for waiting until the processor reaches a certain percentage

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();
}

Solution 9 - C#

I did not like having to add in the 1 second stall to all of the PerformanceCounter solutions. Instead I chose to use a WMI solution. The reason the 1 second wait/stall exists is to allow the reading to be accurate when using a PerformanceCounter. However if you calling this method often and refreshing this information, I'd advise not to constantly have to incur that delay... even if thinking of doing an async process to get it.

I started with the snippet from here https://stackoverflow.com/questions/9777661/returning-cpu-usage-in-wmi-using-c-sharp and added a full explanation of the solution on my blog post below:

Get CPU Usage Across All Cores In C# Using WMI

Solution 10 - C#

public int GetCpuUsage()
{
    var cpuCounter = new PerformanceCounter("Processor", "% Processor Time", "_Total", Environment.MachineName);
    cpuCounter.NextValue();
    System.Threading.Thread.Sleep(1000); //This avoid that answer always 0
    return (int)cpuCounter.NextValue();
}

Original information in this link https://gavindraper.com/2011/03/01/retrieving-accurate-cpu-usage-in-c/

Solution 11 - C#

For those who still could not get the total CPU usage figure which matches Task Manager, you should use this statement:

new PerformanceCounter("Processor Information", "% Processor Utility", "_Total");

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionGrace View Question on Stackoverflow
Solution 1 - C#Christian C. SalvadóView Answer on Stackoverflow
Solution 2 - C#Khalid RahamanView Answer on Stackoverflow
Solution 3 - C#MtnManChrisView Answer on Stackoverflow
Solution 4 - C#xoxoView Answer on Stackoverflow
Solution 5 - C#adparadoxView Answer on Stackoverflow
Solution 6 - C#TarksView Answer on Stackoverflow
Solution 7 - C#Colin BreameView Answer on Stackoverflow
Solution 8 - C#Jay Byford-RewView Answer on Stackoverflow
Solution 9 - C#atconwayView Answer on Stackoverflow
Solution 10 - C#araad1992View Answer on Stackoverflow
Solution 11 - C#AecorSoftView Answer on Stackoverflow