vb.net 如何获取CPU使用率?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/796430/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 14:12:07  来源:igfitidea点击:

How to get CPU usage?

vb.net

提问by Mark

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

如何让 cpu 使用百分比显示在表单的标签中?

回答by 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()

回答by Index

codekaizen said:

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()

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:

如果您最终得到“0”(可能是因为您刚刚创建了 PerformanceCounter 然后直接使用它),您需要添加 2 行,因为 PerformanceCounter 需要一些时间才能工作:

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.

为了避免这种睡眠,您可能需要在类中声明 PerformanceCounter 而不是 Sub/Function 并在表单加载事件中设置概率。

回答by Gerrie Schenck

Look here: How to get the CPU Usage C#

看这里:如何获取 CPU 使用率 C#

Should be easy to translate to VB.Net

应该很容易翻译成VB.Net

回答by Sean Turner

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.

您至少可以使用 WMI API 在 .NET 中完成。WMI 允许您获取一堆 Windows 管理类型的数据,例如 CPU 使用率、硬件规格等。

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

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