0

I found a tutorial on the internet about how to make a CPU meter in vb, but I want to make it count the process of all the cores I have. I have a dual core PC, but let's say that I want this program to detect how many cores I have, and count the processes on each individual core.

Now I have this code and I have no idea why it returns a single value, because I have two cores:

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

Sorry, I forgot to say something about PerformanceCounter1:

  • CategoryName: Thread (I'm not shure if this helps but this is what the guy from the tutorial did)

  • CounterName: % processor time

  • InstanceName: idle/0

Brian Rasmussen
  • 114,645
  • 34
  • 221
  • 317
Victor
  • 109
  • 1
  • 14

1 Answers1

0

Please post the code where you declared Performancecounter1. I believe you are pulling back a total of the thread for all logical processors. To get where you're going, you will need to have some separate labels and counters.

Here's an example:

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

Original code I borrowed from here

Here's a prior question on this topic, but in C#

Community
  • 1
  • 1
Jimmy Smith
  • 2,452
  • 1
  • 16
  • 19