This documentation is archived and is not being maintained.

PerformanceCounter.NextValue Method

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.


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

.NET Framework

Supported in: 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.