PerformanceCounter.NextSample 方法

定义

获取计数器样本,并为其返回原始值(即未经过计算的值)。Obtains a counter sample, and returns the raw, or uncalculated, value for it.

public:
 System::Diagnostics::CounterSample NextSample();
public System.Diagnostics.CounterSample NextSample ();
member this.NextSample : unit -> System.Diagnostics.CounterSample
Public Function NextSample () As CounterSample

返回

CounterSample

一个 CounterSample,它代表系统为此计数器获取的下一原始值。A CounterSample that represents the next raw value that the system obtains for this counter.

例外

此实例未与性能计数器正确关联。The instance is not correctly associated with a performance counter.

- 或 --or- 使用全局共享内存时,InstanceLifetime 属性设置为 ProcessThe InstanceLifetime property is set to Process when using global shared memory.

访问 API 时出错。An error occurred when accessing a system API.

以不具有管理特权的用户身份正在执行的代码尝试读取性能计数器。Code that is executing without administrative privileges attempted to read a performance counter.

示例

下面的代码示例演示如何使用 NextSample 方法来获取计数器的下一个未计算值。The following code example demonstrates how to use the NextSample method to obtain the next uncalculated value of a counter. 此代码示例是类的更大示例的一部分 PerformanceCounterThis code example is part of a larger example for the PerformanceCounter class.

void CollectSamples( ArrayList^ samplesList, PerformanceCounter^ PC, PerformanceCounter^ BPC )
{
   Random^ r = gcnew Random( DateTime::Now.Millisecond );

   // Loop for the samples.
   for ( int j = 0; j < 100; j++ )
   {
      int value = r->Next( 1, 10 );
      Console::Write( "{0} = {1}", j, value );
      PC->IncrementBy( value );
      BPC->Increment();
      if ( (j % 10) == 9 )
      {
         OutputSample( PC->NextSample() );
         samplesList->Add( PC->NextSample() );
      }
      else
            Console::WriteLine();
      System::Threading::Thread::Sleep( 50 );
   }
}
private static void CollectSamples(ArrayList samplesList)
{

    Random r = new Random( DateTime.Now.Millisecond );

    // Loop for the samples.
    for (int j = 0; j < 100; j++)
    {

        int value = r.Next(1, 10);
        Console.Write(j + " = " + value);

        avgCounter64Sample.IncrementBy(value);

        avgCounter64SampleBase.Increment();

        if ((j % 10) == 9)
        {
            OutputSample(avgCounter64Sample.NextSample());
            samplesList.Add( avgCounter64Sample.NextSample() );
        }
        else
        {
            Console.WriteLine();
        }

        System.Threading.Thread.Sleep(50);
    }
}
Private Shared Sub CollectSamples(ByVal samplesList As ArrayList)

    Dim r As New Random(DateTime.Now.Millisecond)

    ' Loop for the samples.
    Dim j As Integer
    For j = 0 To 99

        Dim value As Integer = r.Next(1, 10)
        Console.Write(j.ToString() + " = " + value.ToString())

        avgCounter64Sample.IncrementBy(value)

        avgCounter64SampleBase.Increment()

        If j Mod 10 = 9 Then
            OutputSample(avgCounter64Sample.NextSample())
            samplesList.Add(avgCounter64Sample.NextSample())
        Else
            Console.WriteLine()
        End If
        System.Threading.Thread.Sleep(50)
    Next j
End Sub

注解

此方法通常用于包含未计算的值的计数器。This method is generally used for counters that contain uncalculated values.

备注

如果属性的值 InstanceLifetimeProcess ,并且性能计数器类别是使用 .NET Framework 版本1.0 或1.1 创建的,则 InvalidOperationException 会引发。If the value for the InstanceLifetime property is Process and the performance counter category was created with .NET Framework version 1.0 or 1.1, an InvalidOperationException is thrown. 使用早期版本创建的性能计数器类别使用全局共享内存,并且的值 InstanceLifetime 必须是 GlobalPerformance counter categories created with earlier versions use global shared memory, and the value for InstanceLifetime must be Global. 如果在 .NET Framework 的版本1.0 或1.1 上运行的应用程序未使用该类别,请删除并重新创建该类别。If the category is not used by applications running on versions 1.0 or 1.1 of the .NET Framework, delete and recreate the category.

备注

若要在 Windows Vista、Windows XP Professional x64 Edition 或 Windows Server 2003 中读取性能计数器,你必须是 "性能监视器用户" 组的成员或具有管理权限。To read performance counters in Windows Vista, Windows XP Professional x64 Edition, or Windows Server 2003, you must either be a member of the Performance Monitor Users group or have administrative privileges.

若要避免提升权限来访问 Windows Vista 中的性能计数器,请将自己添加到性能监视器用户组。To avoid having to elevate your privileges to access performance counters in Windows Vista, add yourself to the Performance Monitor Users group.

在 Windows Vista 中,用户帐户控制 (UAC) 决定用户的特权。In Windows Vista, User Account Control (UAC) determines the privileges of a user. 如果您是内置的 Administrators 组的成员,将为您分配两个运行时访问令牌:一个标准用户访问令牌和一个管理员访问令牌。If you are a member of the Built-in Administrators group, you are assigned two run-time access tokens: a standard user access token and an administrator access token. 默认情况下,您拥有标准用户角色。By default, you are in the standard user role. 若要执行访问性能计数器的代码,必须首先将你的特权从标准用户提升到管理员。To execute the code that accesses performance counters, you must first elevate your privileges from standard user to administrator. 你可以通过以下方式执行此操作:右键单击应用程序图标并指示需以管理员身份运行。You can do this when you start an application by right-clicking the application icon and indicating that you want to run as an administrator.

适用于