2014-10-30 39 views
1

警告是:創建表單時發生錯誤。有關詳細信息,請參閱Exception.InnerException。錯誤是:無法加載計數器名稱數據,因爲從註冊表中讀取了無效索引''。VB.NET警告,CPU應用程序

我的代碼是:

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 
+0

你應該通過啓用[選項嚴格開]開頭(http:// MSDN .microsoft.com/en-us/library/zcd4xwzs.aspx)並更正它向您顯示的錯誤。 – 2014-10-30 20:46:53

+0

...如果您告訴我們您用於「PerformanceCounter1」的參數,這將有所幫助。 – 2014-10-30 20:53:38

+0

[另請參閱](http://stackoverflow.com/a/23727302/1070452) – Plutonix 2014-10-30 21:32:15

回答

1

看一看this question。我發現這個,還有更多的例子表明你的問題是由一個或多個損壞的註冊表項造成的。 Pablissimo的答案提供了問題的解釋以及重建這些條目的相關步驟。

單擊開始,鍵入cmd,右鍵單擊cmd.exe,然後選擇以管理員身份運行 。在提示符下,鍵入lodctr/r並按ENTER鍵。

0

所有CPU計數器首先給出了我的電腦一個奇怪的值。

我用的也是性能計數器,但該數值幾乎等同於Windows「比它高2倍」任務管理器。

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 

另一件事是...
你的代碼來獲取ProcessorNameString和Mhz的工作在我的電腦上..

但你也可以使用它像這樣。

你需要進口的Microsoft.Win32「當然你已經有一個」

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 

如果這不工作,你也可以使用WMI「ManagementObjectSearcher」它。

您需要進口System.Management
並將參考System.Management添加到您的項目。

enter image description here enter image description here

,那麼你可以使用此代碼的形式加載事件來獲得相同的信息

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