# PerformanceCounter.NextValue Method

## Definition

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

``````public:
float NextValue();``````
``public float NextValue ();``
``member this.NextValue : unit -> single``
``Public Function NextValue () As Single``

#### Returns

Single

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

#### Exceptions

The instance is not correctly associated with a performance counter.

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 creates an ElapsedTime counter and uses the NextValue method to display the counter's values over a time period.

``````#using <System.dll>

using namespace System;
using namespace System::Collections;
using namespace System::Collections::Specialized;
using namespace System::Diagnostics;
using namespace System::Runtime::InteropServices;

void OutputSample( CounterSample s )
{
Console::WriteLine( "\r\n+++++++++++" );
Console::WriteLine( "Sample values - \r\n" );
Console::WriteLine( "   BaseValue        = {0}", s.BaseValue );
Console::WriteLine( "   CounterFrequency = {0}", s.CounterFrequency );
Console::WriteLine( "   CounterTimeStamp = {0}", s.CounterTimeStamp );
Console::WriteLine( "   CounterType      = {0}", s.CounterType );
Console::WriteLine( "   RawValue         = {0}", s.RawValue );
Console::WriteLine( "   SystemFrequency  = {0}", s.SystemFrequency );
Console::WriteLine( "   TimeStamp        = {0}", s.TimeStamp );
Console::WriteLine( "   TimeStamp100nSec = {0}", s.TimeStamp100nSec );
Console::WriteLine( "++++++++++++++++++++++" );
}

void CollectSamples()
{
String^ categoryName = "ElapsedTimeSampleCategory";
String^ counterName = "ElapsedTimeSample";

// Create the performance counter category.
if (  !PerformanceCounterCategory::Exists( categoryName ) )
{
CounterCreationDataCollection^ CCDC = gcnew CounterCreationDataCollection;

CounterCreationData^ ETimeData = gcnew CounterCreationData;
ETimeData->CounterType = PerformanceCounterType::ElapsedTime;
ETimeData->CounterName = counterName;

// Create the category.
PerformanceCounterCategory::Create( categoryName,
"Demonstrates ElapsedTime performance counter usage.",
CCDC );
}
else
{
Console::WriteLine( "Category exists - {0}", categoryName );
}

// Create the performance counter.
PerformanceCounter^ PC = gcnew PerformanceCounter( categoryName,
counterName,
false );
// 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() = {0}", PC->NextValue() );
Console::WriteLine( "Actual elapsed time = {0}", DateTime::Now.Subtract( Start ) );
OutputSample( PC->NextSample() );
}

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

Console::WriteLine( "Elapsed time = {0}", DateTime::Now.Subtract( Start ) );
}

int main()
{
CollectSamples();
}
``````
``````
using System;
using System.Collections;
using System.Collections.Specialized;
using System.Diagnostics;
using System.Runtime.InteropServices;

public class App
{

public static void Main()
{
CollectSamples();
}

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();

CounterCreationData ETimeData = new CounterCreationData();
ETimeData.CounterType = PerformanceCounterType.ElapsedTime;
ETimeData.CounterName = counterName;

// Create the category.
PerformanceCounterCategory.Create(categoryName,
"Demonstrates ElapsedTime performance counter usage.",
PerformanceCounterCategoryType.SingleInstance, CCDC);
// Return, rerun the application to make use of the new counters.
return;
}
else
{
Console.WriteLine("Category exists - {0}", categoryName);
}

// Create the performance counter.
PerformanceCounter PC = new PerformanceCounter(categoryName,
counterName,
false);
// 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());
OutputSample(PC.NextSample());
}

// 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("\r\n+++++++++++");
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);
Console.WriteLine("++++++++++++++++++++++");
}
}
``````
``````
Imports System.Collections
Imports System.Collections.Specialized
Imports System.Diagnostics
Imports System.Runtime.InteropServices

Public Class App

Public Shared Sub Main()
CollectSamples()
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()

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)

Else
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()))
OutputSample(PC.NextSample())
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 = " + _
DateTime.Now.Subtract(Start).ToString()))
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 = " + _
s.CounterFrequency.ToString()))
Console.WriteLine(("   CounterTimeStamp = " + _
s.CounterTimeStamp.ToString()))
Console.WriteLine(("   CounterType      = " + _
s.CounterType.ToString()))
Console.WriteLine(("   RawValue         = " + _
s.RawValue.ToString()))
Console.WriteLine(("   SystemFrequency  = " + _
s.SystemFrequency.ToString()))
Console.WriteLine(("   TimeStamp        = " + _
s.TimeStamp.ToString()))
Console.WriteLine(("   TimeStamp100nSec = " + _
s.TimeStamp100nSec.ToString()))

Console.WriteLine("+++++++")
End Sub
End Class
``````

## Remarks

Note

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.

Note
{"ft_lang_label":"__label__en","ft_lang_prob":0.51313186,"math_prob":0.9471906,"size":9582,"snap":"2021-43-2021-49","text_gpt3_token_len":2099,"char_repetition_ratio":0.20181666,"word_repetition_ratio":0.037197232,"special_character_ratio":0.24587768,"punctuation_ratio":0.24718356,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.95954674,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2021-12-07T01:34:36Z\",\"WARC-Record-ID\":\"<urn:uuid:c11eecbd-2856-44e6-8ab7-f314489760c1>\",\"Content-Length\":\"57807\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:fba0074c-61ab-4abb-9d82-ebf7b4061606>\",\"WARC-Concurrent-To\":\"<urn:uuid:32fdac7d-67ab-4540-bd97-7b43ee3b29b4>\",\"WARC-IP-Address\":\"23.208.54.245\",\"WARC-Target-URI\":\"https://docs.microsoft.com/en-us/dotnet/api/system.diagnostics.performancecounter.nextvalue?view=windowsdesktop-5.0\",\"WARC-Payload-Digest\":\"sha1:WF6KFDG6SU44VNUK3A3DZ3RZFXFUEVBF\",\"WARC-Block-Digest\":\"sha1:UWLYFEUVT4TRXQSGCSDOD2HW5OOHBGYH\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2021/CC-MAIN-2021-49/CC-MAIN-2021-49_segments_1637964363327.64_warc_CC-MAIN-20211206224536-20211207014536-00519.warc.gz\"}"}