PerformanceCounter.NextSample Method

Definition

Obtains a counter sample, and returns the raw, or uncalculated, value for it.

C#
public System.Diagnostics.CounterSample NextSample();

Returns

A CounterSample that represents the next raw value that the system obtains for this counter.

Exceptions

The instance is not correctly associated with a performance counter.

-or-

The InstanceLifetime property is set to Process when using global shared memory.

An error occurred when accessing a system API.

Code that is executing without administrative privileges attempted to read a performance counter.

Examples

The following code example demonstrates how to use the NextSample method to obtain the next uncalculated value of a counter. This code example is part of a larger example for the PerformanceCounter class.

C#
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);
    }
}

Remarks

This method is generally used for counters that contain uncalculated values.

Note

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. Performance counter categories created with earlier versions use global shared memory, and the value for InstanceLifetime must be Global. 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.

Note

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.

To avoid having to elevate your privileges to access performance counters in Windows Vista, add yourself to the Performance Monitor Users group.

In Windows Vista, User Account Control (UAC) determines the privileges of a user. 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.

Applies to

Product Versions
.NET 8 (package-provided), 9 (package-provided), 10 (package-provided)
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0 (package-provided)
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9, 10