CSharpFeeds - All your C# feeds in one place.

Sponsors

Tuesday, June 06, 2006

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

by BCLTeam via BCL Team Blog on 6/6/2006 3:24:00 PM

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.

 

email it!bookmark it!digg it!

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

Subscribe

New Feed

Product Spotlight

Recently Updated Sources

Legal Note

The content of the postings is owned by the respective author. CSharpFeeds is not responsible for the contents of the postings. This site is automatically generated and cannot be reviewed for abusive content. If you find abusive content on CSharpFeeds, please contact us. Designated trademarks and brands are the property of their respective owners. All rights reserved.

Advertise with us