Visual Studio - 单个内核的CPU计量器(.net)

时间:2014-05-27 15:05:07

标签: .net vb.net cpu

我在互联网上找到了一个关于如何在vb中制作CPU仪表的教程,但是我想让它计算我拥有的所有核心的过程。我有一台双核PC,但是我要说我希望这个程序能够检测出我拥有多少核心,并计算每个核心的进程。

现在我有了这段代码,我不知道它为什么会返回一个值,因为我有两个核心:

Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
    Dim cpuLoad As Integer = CDec(PerformanceCounter1.NextValue.ToString())
    cpuLoad = 100 - cpuLoad
    Label1.Text = cpuLoad.ToString() & "%"
    On Error Resume Next
    ProgressBar1.Value = cpuLoad.ToString()
End Sub

抱歉,我忘了谈一下PerformanceCounter1:

  • CategoryName:Thread(如果这有帮助的话我不是很好,但这就是教程中的人所做的那样)

  • CounterName:%处理器时间

  • InstanceName:idle / 0

1 个答案:

答案 0 :(得分:0)

请发布您声明Performancecounter1的代码。我相信你正在撤回所有逻辑处理器的总线程。要到达目的地,您需要有一些单独的标签和计数器。

以下是一个例子:

Dim PcList As New List(Of PerformanceCounter)
PcList.Add(New PerformanceCounter("Processor", "% Processor Time", "_Total", True)) 'Believe that's all you currently get, TOTAL
For i = 0 To Environment.ProcessorCount - 1
    'Create a counter for each logical processor
    PcList.Add(New PerformanceCounter("Processor", "% Processor Time", i, True)) ' Core i
Next

Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
        For Each Pc In PcList
            Label1.Text += Pc.InstanceName & "|" & 100 - CDec(Pc.NextValue.ToString) & vbCrLf
        Next
    End Sub

我从here

借来的原始代码

这里是先前的question on this topic,但是在C#