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: 
4

1 回答 1

1

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

于 2013-12-02T07:06:18.000 回答