.net 为什么此性能监视器代码失败
这段代码过去可以工作,现在它抛出异常并拒绝工作。代码没有改变。我怀疑原因是我将磁盘分区以添加第二个分区。其目的是监视其自身路径所在的物理驱动器的空闲时间百分比.net 为什么此性能监视器代码失败,.net,system.diagnostics,performancecounter,.net,System.diagnostics,Performancecounter,这段代码过去可以工作,现在它抛出异常并拒绝工作。代码没有改变。我怀疑原因是我将磁盘分区以添加第二个分区。其目的是监视其自身路径所在的物理驱动器的空闲时间百分比 Try Dim drive As String = BunzipInputStream.BinDirectory.Substring(0, 2) Dim instname As String = Nothing If drive.EndsWith(":"c, StringCompar
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()不可显示,则有一个计时器会调用它)
正如Hans Passant在评论中所说,您的变量instname可能没有按照您的预期进行设置 哪一个?我可以阅读任何.NET家族。@TomaszNurkiewicz它已经被标记为.NET。虽然代码是VB,但问题很可能与任何特定于VB的内容无关。引发的异常是什么?这可能会有所帮助:For循环未找到匹配项,因此instname变量仍然为空。很容易在调试器中看到。那么,@HansPassant应该提供一个答案吗?或者,至少是问题的作者是的。汉斯需要提供答案。
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: