此代码曾经工作,现在它抛出异常并拒绝工作。代码没有改变。我怀疑原因是我将磁盘分区以添加第二个分区。预期目的是监视物理驱动器自身路径所在的%空闲时间。
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 :(得分:1)
正如Hans Passant在评论中所说,你的变量 instname 可能没有像你想要的那样设置。