如何获取CPU使用率?

问题描述:

如何获得cpu使用百分比以显示在表单上的标签上?如何获取CPU使用率?

+2

VB.NET或Visual Basic 6.0? – 2009-04-28 06:15:41

看吧:How to get the CPU Usage C#

应该很容易转换为VB.Net

你至少可以使用WMI API做它在.NET。 WMI可以让你得到了一组Windows管理类型的数据,如CPU使用率,hardare规格等

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

Import Namespace System.Diagnostics 

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

' ... 
myLabel.Text = cpu.NextValue() 
+0

thanx帮助 – Mark 2011-04-11 23:10:16

codekaizen说:

Import Namespace System.Diagnostics 

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

myLabel.Text = cpu.NextValue() 

如果你结束了“0”(可能是因为你刚刚创建了PerformanceCounter,然后直接使用它) 由于PerformanceCounter需要一些工作时间,所以需要添加2行:

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

为了避免睡眠,您可能需要在Class中声明PerformanceCounter而不是Sub/Function,并在窗体加载事件中设置probs。