PerformanceCounter.NextValue Method ()


The .NET API Reference documentation has a new home. Visit the .NET API Browser on to see the new experience.

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

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

Public Function NextValue As Single

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.

Imports System
Imports System.Collections
Imports System.Collections.Specialized
Imports System.Diagnostics
Imports System.Runtime.InteropServices
Imports Microsoft.VisualBasic

Public Class App

    Public Shared Sub Main()
    End Sub

    Private Shared Sub CollectSamples()

        Dim categoryName As String = "ElapsedTimeSampleCategory"
        Dim counterName As String = "ElapsedTimeSample"

        If Not PerformanceCounterCategory.Exists(categoryName) Then

            Dim CCDC As New CounterCreationDataCollection()

            ' Add the counter.
            Dim ETimeData As New CounterCreationData()
            ETimeData.CounterType = PerformanceCounterType.ElapsedTime
            ETimeData.CounterName = counterName

            ' Create the category.
            PerformanceCounterCategory.Create(categoryName, _
               "Demonstrates ElapsedTime performance counter usage.", _
                   PerformanceCounterCategoryType.SingleInstance, CCDC)

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

        ' Create the counter.
        Dim PC As PerformanceCounter
        PC = New PerformanceCounter(categoryName, counterName, False)

        ' Initialize the counter.
        PC.RawValue = Stopwatch.GetTimestamp()

        Dim Start As DateTime = DateTime.Now

        ' Loop for the samples.
        Dim j As Integer
        For j = 0 To 99
            ' Output the values.
            If j Mod 10 = 9 Then
                Console.WriteLine(("NextValue() = " _
                    + PC.NextValue().ToString()))
                Console.WriteLine(("Actual elapsed time = " _
                    + DateTime.Now.Subtract(Start).ToString()))
            End If

            ' Reset the counter every 20th iteration.
            If j Mod 20 = 0 Then
                PC.RawValue = Stopwatch.GetTimestamp()
                Start = DateTime.Now
            End If
        Next j

        Console.WriteLine(("Elapsed time = " + _
    End Sub

    Private Shared Sub OutputSample(ByVal s As CounterSample)
        Console.WriteLine(ControlChars.Lf + ControlChars.Cr + "+++++++")

        Console.WriteLine("Sample values - " + ControlChars.Cr _
              + ControlChars.Lf)
        Console.WriteLine(("   BaseValue        = " _
              + s.BaseValue.ToString()))
        Console.WriteLine(("   CounterFrequency = " + _
        Console.WriteLine(("   CounterTimeStamp = " + _
        Console.WriteLine(("   CounterType      = " + _
        Console.WriteLine(("   RawValue         = " + _
        Console.WriteLine(("   SystemFrequency  = " + _
        Console.WriteLine(("   TimeStamp        = " + _
        Console.WriteLine(("   TimeStamp100nSec = " + _

    End Sub
End Class


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

.NET Framework
Available since 1.1
Return to top