6

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

Mat
  • 202,337
  • 40
  • 393
  • 406
Mark
  • 117
  • 1
  • 1
  • 8

4 Answers4

21
Import Namespace System.Diagnostics

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

' ...
myLabel.Text = cpu.NextValue()
codekaizen
  • 26,990
  • 7
  • 84
  • 140
4

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.

Celeo
  • 5,583
  • 8
  • 39
  • 41
Index
  • 225
  • 1
  • 9
1

Look here: How to get the CPU Usage C#

Should be easy to translate to VB.Net

Community
  • 1
  • 1
Gerrie Schenck
  • 22,148
  • 20
  • 68
  • 95
1

You can do it in .NET at least using the WMI API. WMI allows you to get a bunch of Windows Management type data such as CPU usage, hardare specs, etc.

http://www.aspfree.com/c/a/VB.NET/WMI-Programming-with-Visual-BasicNET-What-is-the-WQL/

Sean Turner
  • 1,178
  • 2
  • 10
  • 14