Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Wednesday, September 26, 2012 9:33 PM
Hello I wanna get the percentage of usage for a CPU Multicore in c#, I found a lot of information but nobody can help me! Somebody have an example of code?
remeber is the percentage of usage for a CPU MULTICORE!!
Thank you! :)
All replies (2)
Friday, September 28, 2012 12:54 AM âś…Answered
Here give this a try,
To get the entire PC CPU and Memory usage:
import System.Diagnostics;
Then declare globally:
private PerformanceCounter theCPUCounter =
new PerformanceCounter("Processor", "% Processor Time", "_Total");
Then to get the CPU time, simply call the NextValue() method:
this.theCPUCounter.NextValue();
This will get you the CPU usage
As for memory usage, same thing applies I believe:
private PerformanceCounter theMemCounter =
new PerformanceCounter("Memory", "Available MBytes");
Then to get the memory usage, simply call the NextValue() method:
this.theMemCounter.NextValue();
For a specific process CPU and Memory usage:
private PerformanceCounter theCPUCounter =
new PerformanceCounter("Process", "% Processor Time",
Process.GetCurrentProcess().ProcessName);
where Process.GetCurrentProcess().ProcessName is the process name you wish to get the information about.
private PerformanceCounter theMemCounter =
new PerformanceCounter("Memory", "Available MBytes",
Process.GetCurrentProcess().ProcessName);
where Process.GetCurrentProcess().ProcessName is the process name you wish to get the information about. Might need to change "Available MBytes" to "Working Set" for the Memory counter if it doesn't work.
To retrieve all Categories, see Walkthrough: Retrieving Categories and Counters
The difference between Processor\% Processor Time and Process\% Processor Time isProcessor is from the PC itself and Process is per individual process. So the processor time of the processor would be usage on the PC. Processor time of a process would be the specified processes usage. For full description of category names: Performance Monitor Counters
An alternative to using the Performance Counter
Use System.Diagnostics.Process.TotalProcessorTime andSystem.Diagnostics.ProcessThread.TotalProcessorTime properties to calculate your processor usage as this article describes.
Friday, September 28, 2012 11:40 PM
Thank you so much!! It's done now!! :)