如何:检索性能计数器样本

更新:2007 年 11 月

可以使用 CounterSample 类创建样本,并对其内容执行计算。取样类根据您定义的判据执行性能计数器的“取样”。这些判据包括来自一个或多个计数器的值以及应提供值的频率。此外,该类还记录下取样的时间。可以在一个实例化类中收集所有这些数据,然后使用 Calculate 方法执行计算。也可以用 Calculate 方法执行计算,该计算比较两个不同取样中的值。

执行的计算取决于计数器的类型;某些计数器类型有与之相关的特定计算。例如,ElapsedTime 类型的计数器通过比较两个不同样本上的时间戳,来确定已经过了多长时间。许多计数器根据所检索的数据执行求平均值的计算。

以下是定义一个取样所要完成的步骤:

  • 创建 CounterSample 类的一个或多个实例。

  • 检索每个实例的当前取样。

  • 调用 Calculate 方法,将要包括在计算中的每个取样作为参数传递。

检索性能计数器取样并执行计算

  1. 创建一个 PerformanceCounter 实例,并将它配置为与所需的类别和计数器进行交互。有关更多信息,请参见如何:创建 PerformanceCounter 组件实例如何:配置 PerformanceCounter 组件实例

  2. 创建 CounterSample 类的实例来保存取样的结果。

  3. 调用 PerformanceCounter 组件实例的 NextSample 方法以检索计算值,并将结果赋给 CounterSample 类。

    提示:

    要执行计算,必须检索两个样本。

  4. 创建一个变量来保存计算结果,并给予它 Single 数据类型。

  5. 对于要用于计算的两个样本中的每一个,分别将 NextSample 的返回值赋给类型为 CounterSample 的变量。

  6. 调用 CounterSample 类的 Calculate 方法,并执行下列两项操作之一:

    • 如果已检索到两个样本,将这两个样本(存储为 CounterSample 对象)都作为参数传递给 Calculate 方法。

    • 如果只检索到一个样本,则将第一个样本传递给 Calculate 方法,然后使用第二个参数来检索另一个样本。

  7. 设置计算结果,使之等于已创建的用于包含结果的变量。

    说明:

    这两个样本必须来自同一类型的计数器,否则该方法会引发异常。计数器的类型确定执行哪种计算。有关更多信息,请参见性能计数器类型

    以下代码阐释如何检索两个取样并用 Calculate 方法对它们进行比较:

    ' Dim variables of types CounterSample for each sample and a 
    ' variable of type single for the results.
    Dim sample1 As CounterSample
    Dim sample2 As CounterSample
    Dim result As Single
    ' Retrieve a sample.
    sample1 = PerformanceCounter1.NextSample()
    ' Wait some interval of time here and retrieve
    ' a second sample.
    System.Threading.Thread.Sleep(1000)
    sample2 = PerformanceCounter1.NextSample()
    ' Pass both samples to the Calculate method.
    result = CounterSample.Calculate(sample1, sample2)
    
         System.Diagnostics.CounterSample sample1;
            System.Diagnostics.CounterSample sample2;
            float result;
            // Retrieve a sample.
            sample1 = PerformanceCounter1.NextSample();
            // Wait some interval of time here and retrieve
            // a second sample.
            System.Threading.Thread.Sleep(1000);
            sample2 = PerformanceCounter1.NextSample();
            // Pass both samples to the Calculate method.
            result = System.Diagnostics.CounterSample.Calculate(sample1, sample2);
    

    或者,您也可以调用 NextSample 方法来提供第二个样本的值。下面的示例阐释如何使用这种方式:

    Dim sample1 As CounterSample
    Dim result As Single
    ' Retrieve a single sample.
    sample1 = PerformanceCounter1.NextSample()
    ' Pass the retrieved sample to the calculate method
    ' and retrieve another sample in the second parameter.
    result = CounterSample.Calculate(sample1, PerformanceCounter1.NextSample())
    
         System.Diagnostics.CounterSample sample1;
            float result;
            // Retrieve a single sample.
            sample1 = PerformanceCounter1.NextSample();
            // Pass the retrieved sample to the calculate method
            // and retrieve another sample in the second parameter.
            result = System.Diagnostics.CounterSample.Calculate(sample1, PerformanceCounter1.NextSample());
    

请参见

任务

如何:检索原始性能计数器值

如何:检索计算所得的性能计数器值

如何:检索计数器列表和类别列表

概念

性能计数器值检索