Welcome to MSDN Blogs Sign in | Join | Help

How to find CPU usage of a process? [Ravi Krishnaswamy]

I’ve seen this question come up a few times and the solution is hard to infer, especially given that the logical place you go to, the Process class, doesn’t have a property to reveal this information. We could look into adding it to Process class at some point.

 

Win32 reveals this information via a performance counter. You can query the “% Processor time” windows counter for a process that you are interested in as follows:

 

foreach (Process proc in Process.GetProcesses()) {
    using (PerformanceCounter pcProcess = new PerformanceCounter("Process", "% Processor Time", proc.ProcessName)) {
        pcProcess.NextValue();
        System.Threading.Thread.Sleep(1000);
        Console.WriteLine("Process:{0} CPU% {1}", proc.ProcessName, pcProcess.NextValue());   
    }
}

 

For an explanation on why two calls to NextValue are necessary and why there must we must wait between them see Ryan's How to Read Performance Counters blog entry.

 

Published Tuesday, June 06, 2006 11:24 AM by BCLTeam

Comments

Tuesday, June 06, 2006 2:31 PM by Dotnet User

# re: How to find CPU usage of a process? [Ravi Krishnaswamy]

Whats the difference between this code and TASK Manager's Process Tab's "CPU Usage"??
Wednesday, June 07, 2006 4:26 AM by virtualblackfox

# More than one process with the same ProcessName

How could you make this code to work when you have more than one process with the same ProcessName (10 instances of some text editor for exemple) ???
Wednesday, June 07, 2006 8:41 AM by Speedbird186

# re: How to find CPU usage of a process? [Ravi Krishnaswamy]

It seems that you haven't read the previous post on this blog.

Ryan explained that you need to call .NextValue() twice, preferably with about a second interval in order to get a correct reading.
Wednesday, June 07, 2006 9:33 AM by Jason Haley

# Interesting Finds: June 7, 2006 AM edition

Wednesday, June 07, 2006 6:48 PM by tyme

# re: How to find CPU usage of a process? [Ravi Krishnaswamy]

Um....how do you find this in pure .net with no windows calls?(the project i am working on needs no platform specific code in it.)
Wednesday, June 07, 2006 8:20 PM by Anon

# re: How to find CPU usage of a process? [Ravi Krishnaswamy]

I don't see why you can't just use Process.GetCurrentProcess().TotalProcessorTime

What am I missing?

Thursday, June 15, 2006 3:39 AM by Krys

# Processor/Memory/Network Usage

Hi,

Here is an article and source code that show how to retreive:
- processor usage
- network usage
- memory usage

http://devauthority.com/blogs/krys/archive/2006/06/13/PerformanceMonitoring.aspx

It is a very simple class to analyse the whole computer performance
Thursday, June 22, 2006 11:38 AM by BCLTeam

# re: How to find CPU usage of a process? [Ravi Krishnaswamy]

The code previously was incorrect and would always print 0. I update the code to correctly print out the percent CPU utilization.

# BCL Team Blog How to find CPU usage of a process Ravi Krishnaswamy | Insomnia Cure

New Comments to this post are disabled
 
Page view tracker