S
S
Senture2018-06-29 12:56:07
C++ / C#
Senture, 2018-06-29 12:56:07

How to find the % CPU usage of a process?

Hello, I need to find out the CPU usage in % by a certain process, how can I do this?
PS Thank you very much.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Chernyakov, 2018-06-29
@Senture

Use the PerformanceCounter and Process
class. Try to execute the following code for yourself, so that it would be more clear to you how to work with these classes further.

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

The algorithm will be something like this:
  1. Get an instance of the Process class for the desired process
  2. Use PerformanceCounter to get % CPU usage

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question