【问题标题】:Why is this Performance Monitor Code failing为什么此性能监视器代码失败
【发布时间】:2013-01-02 18:28:02
【问题描述】:

这段代码以前可以工作,现在它抛出异常并拒绝工作。代码没有改变。我怀疑原因是当我对磁盘进行分区以添加第二个分区时。预期目的是监控它自己的路径所在的物理驱动器的空闲时间百分比。

    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(),如果它不是Nothing用于显示)。

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: 

【问题讨论】:

  • 哪一个?我可以阅读任何 .NET 系列。
  • @TomaszNurkiewicz 它已经被标记为 .NET。虽然代码是 VB,但问题很可能与任何 VB 相关。
  • 抛出的异常是什么?
  • 这可能会有所帮助:stackoverflow.com/questions/390166/…
  • 您的 For 循环没有找到匹配项,因此您的 instname 变量仍然是 Nothing。顺便说一句,在调试器中很容易看到。

标签: .net system.diagnostics performancecounter


【解决方案1】:

正如 Hans Passant 在 cmets 中所说,您的变量 instname 可能没有按照您想要的方式设置。

【讨论】:

  • 那么,@HansPassant 应该提供答案吗?或者,至少是问题的作者
  • 是的。汉斯需要提供答案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-09-18
  • 1970-01-01
  • 1970-01-01
  • 2020-01-03
  • 1970-01-01
  • 2019-01-19
  • 1970-01-01
相关资源
最近更新 更多