• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 3729
  • Last Modified:

How can I monitor CPU usage in VB.net ?

I would like to know how can I monitor a CPU usage for a specif process using VB.net, I mean, I would like to have the same information that Task Manager provides at CPU column under Process tab.

Thank you,

Jose Roberto
0
jr_barros_jr
Asked:
jr_barros_jr
  • 4
  • 3
  • 2
1 Solution
 
Frosty555Commented:
You want to monitor a performance counter.

You drop a performancecounter object from your toolbox on the form, and set its properties accordingly to get the CPU usage just like you would if you were using Resource Monitor. Then, in your code you repeatedly call counter.NextValue() to get the value of the counter.
0
 
Frosty555Commented:
I believe in your case you want:

Dim PC As New PerformanceCounter()
PC.CategoryName = "Processor"
PC.CounterName = "% Processor Time"
PC.InstanceName = "_Total"
MessageBox.Show(PC.NextValue().ToString())
0
 
jr_barros_jrAuthor Commented:
I copied your code, but the values I am getting are different than the values showed by task manager. Are you sure that PC.CounterName = "% Processor Time" ? I want to monitor CPU usage. I do not want CPU time.
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
barrislbCommented:
Hi jr_barros_jr,

I don't know if this will help you, or not. You may already have this code. The Performance Counter is similar to Frosty555. Give a shot and see. Make sure you set Timer1 Property:  Enable = True and Interval = 1000. You may not get the exact % as the task manager every time, but it seems to be pretty accurate. Anyway, I hope this some help to your need.
'-- Declare Private Member
    Private mPerformanceCounter As New System.Diagnostics.PerformanceCounter("Processor", "% Processor Time", "_Total")
 
    Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click
        lvCPU.Items.Clear()
    End Sub
 
    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        lvCPU.Items.Add(CInt(mPerformanceCounter.NextValue()) & "%")
        lvCPU.SelectedIndex = lvCPU.Items.Count - 1
    End Sub

Open in new window

0
 
barrislbCommented:
lvCPU is a ListBox Control.
0
 
jr_barros_jrAuthor Commented:
Unfortunately, the code was essentially the same and only provided % Processor TIME. I do NOT want processor time and I want the CPU load/usage.

Example: if I run a do/loop without any code inside, this process will have 100% of CPU according to Task Manager. I want to monitor this CPU. I do not want the time.

Task Manager has both and both are different. I want the current (right now) load/usage.

Thank you,

Jose Roberto
0
 
Frosty555Commented:
barros,

Open up performance monitor (start->run->perfmon). Right click on the graph. Go "Add Counters". Inside you will see the various counters that exist on your system.

The "Performance Object" dropdown is equivalent to the "CategoryName" property in .NET.
The item you select from the Left Column is equivalent to the "CounterName" property in .NET.
The item you select from the Right Column is equivalent to the "InstanceName" property in .NET.

Pick whichever counter suits your needs bests.

"Processor->% Processor Time->_Total" is defined as (100 - % Processor Time for Idle Thread). It mathematically should be equal to the sum of all the % Processor Time counters for all the processes on the system.

These are the only measuers of performance that exist in a Windows system. Every other tool in windows that shows CPU usage (e.g. Task Manager) uses these counters.

Processor Time is only part of the big picture when it comes to determining how loaded the cpu is. The amount of time the CPU spends doing DPCs, context switches (time spent switching between tasks), and handling hardware interrupts are also important, and most likely Task Manager does some calculations to average this into account as well.
0
 
Frosty555Commented:
See this reference for what the different counters mean. It's a little more complicated than just "cpu usage".

http://technet.microsoft.com/en-us/library/cc786359.aspx
0
 
jr_barros_jrAuthor Commented:
Thank you so much for your help and your patient.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

  • 4
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now