1

The warning is : An error occurred creating the form. See Exception.InnerException for details. The error is: Cannot load Counter Name data because an invalid index '' was read from the registry.

My Code is :

Imports System
Imports System.Management
Imports System.Diagnostics

Public Class Form1

    Private Sub Timer1_Tick(ByVal sender As Object, ByVal 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

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
        Timer1.Start()

        Label2.Text = My.Computer.Registry.GetValue("HKEY_LOCAL_MACHINE\HARDWARE\DESCRIPTION\SYSTEM\CentralProcessor\0", "ProcessorNameString", Nothing)

        label3.text = My.Computer.Registry.GetValue("HKEY_LOCAL_MACHINE\HARDWARE\DESCRIPTION\SYSTEM\CentralProcessor\0", "~MHz", Nothing) & " Mhz"
    End Sub

End Class
Cᴏʀʏ
  • 105,112
  • 20
  • 162
  • 194
  • You should start by enabling [Option Strict On](http://msdn.microsoft.com/en-us/library/zcd4xwzs.aspx) and correcting the errors it shows you. – Andrew Morton Oct 30 '14 at 20:46
  • ... and it would help if you told us the parameters you've used for `PerformanceCounter1`. – Andrew Morton Oct 30 '14 at 20:53
  • [see also](http://stackoverflow.com/a/23727302/1070452) – Ňɏssa Pøngjǣrdenlarp Oct 30 '14 at 21:32
  • if you use cpuLoad = 100 - cpuLoad then it shows you how much is idle.you can remove "cpuLoad = 100 - cpuLoad" to get the good value.Dim cpuLoad As Integer = CDec(PerformanceCounter1.NextValue.ToString()) why you use .tostring ? you can also remove the .ToString() – Creator Oct 31 '14 at 07:01

2 Answers2

1

Have a look at this question. I found this, and many more examples suggesting that your problem results from one or more corrupted registry entries. Pablissimo's answer provides an explanation of the problem, and the relevant steps to rebuild these entries.

Click Start, type cmd right click cmd.exe, and select Run as administrator. At the prompt, type lodctr /r and press ENTER.

Community
  • 1
  • 1
Justin Ryan
  • 1,409
  • 1
  • 12
  • 25
0

First of all your cpu counter gives a strange value on my pc."2 times higher than it is"

what i use is also a performance counter but the value is almost the same as in windows task manager.

Dim CpuCounter As New PerformanceCounter()
Dim CpuResult As New Integer
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer2.Tick
    With CpuCounter
        .CategoryName = "Processor"
        .CounterName = "% Processor Time"
        .InstanceName = "_Total"
    End With
    CpuResult = CpuCounter.NextValue
    CpuLabel.Text = CpuResult & "%"
    CpuBar.Value = CpuResult
End Sub

the other thing is ...
Your code to get the ProcessorNameString and the Mhz is working on my computer..

But you can also use it like this.

you need to Imports Microsoft.Win32 "but of course you already have that"

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

    Dim cpuName As String
    Dim cpuFreq As String
    Dim regKey As RegistryKey
    regKey = Registry.LocalMachine
    regKey = regKey.OpenSubKey("HARDWARE\DESCRIPTION\System\CentralProcessor\0", False)
    cpuName = regKey.GetValue("ProcessorNameString")
    cpuFreq = regKey.GetValue("~MHz")
    Label2.Text = cpuName
    Label3.Text = cpuFreq & " MHz"

End Sub

And if this does not work you can also use WMI "ManagementObjectSearcher" for it.

you need to Imports System.Management
and Add reference System.Management to your project.

enter image description here enter image description here

then you can use this code in the form load event to get the same info

    Dim cpuName As String
    Dim cpuFreq As String

Try
        Dim searcher As New ManagementObjectSearcher( _
            "root\CIMV2", _
            "SELECT * FROM Win32_Processor")

        For Each queryObj As ManagementObject In searcher.Get()

            cpuName = queryObj("Name")
            cpuFreq = queryObj("CurrentClockSpeed")

            Label2.Text = cpuName
            Label3.Text = cpuFreq & "MHz"
        Next
    Catch err As ManagementException
        MessageBox.Show("An error occurred while querying for WMI data: " & err.Message)
    End Try
Creator
  • 1,502
  • 4
  • 17
  • 30