这段代码以前是可以工作的,现在它抛出异常并拒绝工作。代码没有改变。我怀疑原因是我在对磁盘进行分区时添加了第二个分区。其预期目的是监视物理驱动器的空闲时间%,它自己的路径所在的物理驱动器。
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
(这是设置代码。如果不是Nothing for display,则有一个定时器调用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:developmentredactedredactedFormsPerformanceMon.vb:line XX
InnerException:
正如Hans Passant在评论中所说,您的变量instname可能没有按您希望的方式设置。