Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All

PerformanceCounter.NextValue Method ()


Obtains a counter sample and returns the calculated value for it.

Namespace:   System.Diagnostics
Assembly:  System (in System.dll)

public float NextValue()

Return Value

Type: System.Single

The next calculated value that the system obtains for this counter.

Exception Condition

The instance is not correctly associated with a performance counter.


An error occurred when accessing a system API.


The platform is Windows 98 or Windows Millennium Edition (Me), which does not support performance counters.


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


If the calculated value of a counter depends on two counter reads, the first read operation returns 0.0. Resetting the performance counter properties to specify a different counter is equivalent to creating a new performance counter, and the first read operation using the new properties returns 0.0. The recommended delay time between calls to the NextValue method is one second, to allow the counter to perform the next incremental read.


To read performance counters, you must have administrative privileges. 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.

The following code example creates an ElapsedTime counter and uses the NextValue method to display the counter's values over a time period.

using System;
using System.Collections;
using System.Collections.Specialized;
using System.Diagnostics;
using System.Runtime.InteropServices;

public class App 

    public static void Main()

    public static void CollectSamples()
        const String categoryName = "ElapsedTimeSampleCategory";
        const String counterName = "ElapsedTimeSample";

        // If the category does not exist, create the category and exit.
        // Performance counters should not be created and immediately used.
        // There is a latency time to enable the counters, they should be created
        // prior to executing the application that uses the counters.
        // Execute this sample a second time to use the category.
        if ( !PerformanceCounterCategory.Exists(categoryName) ) 

            CounterCreationDataCollection CCDC = new CounterCreationDataCollection();

            // Add the counter.
            CounterCreationData ETimeData = new CounterCreationData();
            ETimeData.CounterType = PerformanceCounterType.ElapsedTime;
            ETimeData.CounterName = counterName;

            // Create the category.
                    "Demonstrates ElapsedTime performance counter usage.",
                PerformanceCounterCategoryType.SingleInstance, CCDC);
            // Return, rerun the application to make use of the new counters.

            Console.WriteLine("Category exists - {0}", categoryName);

        // Create the performance counter.
        PerformanceCounter PC = new PerformanceCounter(categoryName, 
        // Initialize the counter.
        PC.RawValue = Stopwatch.GetTimestamp();

        DateTime Start = DateTime.Now;

        // Loop for the samples.
        for (int j = 0; j < 100; j++) 
            // Output the values.
            if ((j % 10) == 9) 
                Console.WriteLine("NextValue() = " + PC.NextValue().ToString());
                Console.WriteLine("Actual elapsed time = " + DateTime.Now.Subtract(Start).ToString());

            // Reset the counter on every 20th iteration.
            if (j % 20 == 0)
                PC.RawValue = Stopwatch.GetTimestamp();
                Start = DateTime.Now;

        Console.WriteLine("Elapsed time = " + DateTime.Now.Subtract(Start).ToString());

    private static void OutputSample(CounterSample s)
        Console.WriteLine("Sample values - \r\n");
        Console.WriteLine("   BaseValue        = " + s.BaseValue);
        Console.WriteLine("   CounterFrequency = " + s.CounterFrequency);
        Console.WriteLine("   CounterTimeStamp = " + s.CounterTimeStamp);
        Console.WriteLine("   CounterType      = " + s.CounterType);
        Console.WriteLine("   RawValue         = " + s.RawValue);
        Console.WriteLine("   SystemFrequency  = " + s.SystemFrequency);
        Console.WriteLine("   TimeStamp        = " + s.TimeStamp);
        Console.WriteLine("   TimeStamp100nSec = " + s.TimeStamp100nSec);

for reading the performance counter category. Associated enumeration: PerformanceCounterPermissionAccess.Read.

.NET Framework
Available since 1.1
Return to top
Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
© 2015 Microsoft