Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get CPU usage?

Tags:

vb.net

How do I get the cpu usage percentage to display in the label on a form?

like image 962
Mark Avatar asked Apr 28 '09 06:04

Mark


2 Answers

Import Namespace System.Diagnostics

' ...
Dim cpu as New PerformanceCounter()
With cpu
    .CategoryName = "Processor"
    .CounterName = "% Processor Time"
    .InstanceName = "_Total"
End With

' ...
myLabel.Text = cpu.NextValue()
like image 81
codekaizen Avatar answered Nov 15 '22 14:11

codekaizen


codekaizen said:

Import Namespace System.Diagnostics

Dim cpu as New PerformanceCounter()
With cpu
    .CategoryName = "Processor"
    .CounterName = "% Processor Time"
    .InstanceName = "_Total"
End With

myLabel.Text = cpu.NextValue()

In case you end up with "0" (probably because you just created the PerformanceCounter and then directly used it) you need to add 2 lines as the PerformanceCounter need some time to work:

System.Threading.Thread.Sleep(1000)
myLabel.Text= cpu.NextValue

To Avoid that sleep you might want to declare the PerformanceCounter in your Class instead of your Sub/Function and set the probs in the forms loading event.

like image 25
Index Avatar answered Nov 15 '22 12:11

Index