2013-01-18 43 views
0

此代碼用於工作,現在它拋出異常並拒絕工作。代碼沒有改變。我懷疑原因是當我分區我的磁盤添加第二個分區。預期的目的是監視物理驅動器自身路徑處於打開狀態的百分比空閒時間。爲什麼此性能監視器代碼失敗

Try 
     Dim drive As String = BunzipInputStream.BinDirectory.Substring(0, 2) 
     Dim instname As String = Nothing 
     If drive.EndsWith(":"c, StringComparison.Ordinal) Then 
      Dim dix = New System.Diagnostics.PerformanceCounterCategory 
      dix.CategoryName = "PhysicalDisk" 
      For Each px In dix.GetInstanceNames() 
       If px.EndsWith(drive, StringComparison.OrdinalIgnoreCase) Then 
        instname = px 
       End If 
      Next 
      diskcounter = New System.Diagnostics.PerformanceCounter() 
      diskcounter.CategoryName = "PhysicalDisk" 
      diskcounter.CounterName = "% Idle Time" 
      diskcounter.InstanceName = instname 
      diskcounter.NextValue() 
     End If 
    Catch ex As Exception 
     'ENDS UP HERE AND ABORTS THE MONITOR 
     If Not diskcounter Is Nothing Then 
      diskcounter.Dispose() 
      diskcounter = Nothing 
     End If 
    End Try 

(這是設置代碼。還有調用diskcounter.NextValue(),如果它不是顯示沒有一個定時器)。

System.InvalidOperationException occurred 
    Message=Counter is not single instance, an instance name needs to be specified. 
    Source=System 
    StackTrace: 
     at System.Diagnostics.PerformanceCounter.NextSample() 
     at System.Diagnostics.PerformanceCounter.NextValue() 
     at Redacted.PerformanceMon.OnHandleCreated(EventArgs e) in C:\development\redacted\redacted\Forms\PerformanceMon.vb:line XX 
    InnerException: 
+0

哪一個?我可以閱讀任何.NET系列。 – Joshua

+0

@TomaszNurkiewicz它已經被標記爲.NET。雖然代碼是VB,但問題很可能不與VB特定的任何東西綁定。 –

+0

什麼是拋出異常? –

回答

1

漢斯·帕桑特在評論中說,您的變量instname可能沒有設置你想讓它的方式。

+0

那麼,@HansPassant應該提供一個答案?或者,至少該問題的作者 –

+0

是的。漢斯需要提供答案。 – Joshua

相關問題