Link to home
Start Free TrialLog in
Avatar of jr_barros_jr
jr_barros_jr

asked on

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
Avatar of Frosty555
Frosty555
Flag of Canada image

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.
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())
Avatar of jr_barros_jr
jr_barros_jr

ASKER

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.
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

lvCPU is a ListBox Control.
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
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.
ASKER CERTIFIED SOLUTION
Avatar of Frosty555
Frosty555
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank you so much for your help and your patient.