Calculating the CPU usage of ONE process with VB.NET 2003
-
Hello everybody, I have a big issue that I can't seem to be able to solve. I need to find a SINGLE CPU process (one program that I know the name of), and display it into a label. For example My_program ---> 20% I use (on a timer) Private m_PerformanceCounter As New System.Diagnostics.PerformanceCounter("Processor", "% Processor Time", "_Total") ListBox1.Items.Add(CInt(m_PerformanceCounter.NextValue()) & "%") but I think I get ALL processes added up. I just want ONE of them for example (in pseudocode) if process.name = "My_program" then label1.text=process.CPUUsageTimeOfTHatProcess How can I do it with VB.NET? Any help will be much appreciated. Thank you in advance.
Still trying to find the way
-
Hello everybody, I have a big issue that I can't seem to be able to solve. I need to find a SINGLE CPU process (one program that I know the name of), and display it into a label. For example My_program ---> 20% I use (on a timer) Private m_PerformanceCounter As New System.Diagnostics.PerformanceCounter("Processor", "% Processor Time", "_Total") ListBox1.Items.Add(CInt(m_PerformanceCounter.NextValue()) & "%") but I think I get ALL processes added up. I just want ONE of them for example (in pseudocode) if process.name = "My_program" then label1.text=process.CPUUsageTimeOfTHatProcess How can I do it with VB.NET? Any help will be much appreciated. Thank you in advance.
Still trying to find the way
Of course it's returning the total CPU usage. Look at the line of code your using:
Chaos Machine wrote:
Private m_PerformanceCounter As New System.Diagnostics.PerformanceCounter("Processor", "% Processor Time", "_Total")
On top of this, you don't even need the Performance counters to do this. You can get a processes CPU time using the process' Process object. Checkout the Process class[^] and it's TotalProcessorTime and UserProcessorTime properties.
A guide to posting questions on CodeProject[^]
Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
2006, 2007, 2008