How to get CPU usage?

How to get the percentage of CPU usage that will be displayed in the form label?

+5
source share
4 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()
+19
source

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" (perhaps because you just created a PerformanceCounter and then used it directly) you need to add 2 lines, as the PerformanceCounter will take some time to work:

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

To avoid this dream, you might want to declare a PerformanceCounter in your class instead of your Sub / Function and fix problems in the form load event.

+2
source

: CPU #

VB.Net

+1

You can do this in .NET, at least using the WMI API. WMI allows you to get a bunch of data such as Windows management, such as CPU usage, hard drive specifications, etc.

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

+1
source

All Articles