Export (0) Print
Expand All

PerformanceCounter.NextValue Method

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

[Visual Basic]
Public Function NextValue() As Single
[C#]
public float NextValue();
[C++]
public: float NextValue();
[JScript]
public function NextValue() : float;

Return Value

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

Exceptions

Exception Type Condition
InvalidOperationException The instance is not correctly associated with a performance counter.
Win32Exception An error occurred when accessing a system API.

Remarks

Note   If the calculated value of a counter depends on two counter reads, the first read returns 0.0.

Example

[Visual Basic] 
Imports System
Imports System.Collections
Imports System.Collections.Specialized
Imports System.Diagnostics
Imports System.Runtime.InteropServices
Imports Microsoft.VisualBasic

Public Class App
   Private Shared PC As PerformanceCounter
   
   Public Shared Sub Main()
      Dim samplesList As New ArrayList()
      
      SetupCategory()
      CreateCounters()
      CollectSamples(samplesList)
   End Sub
  
   
   Private Shared Function SetupCategory() As Boolean
      If Not PerformanceCounterCategory.Exists("ElapsedTimeSampleCategory") Then
         
         Dim CCDC As New CounterCreationDataCollection()
         
         ' Add the counter.
         Dim ETimeData As New CounterCreationData()
         ETimeData.CounterType = PerformanceCounterType.ElapsedTime
         ETimeData.CounterName = "ElapsedTimeSample"
         CCDC.Add(ETimeData)
         
         ' Create the category.
         PerformanceCounterCategory.Create("ElapsedTimeSampleCategory", _
            "Demonstrates usage of the ElapsedTime performance counter type.", CCDC)
         
         Return True
      Else
         Console.WriteLine("Category exists - ElapsedTimeSampleCategory")
         Return False
      End If
   End Function 'SetupCategory
   
   
   Private Shared Sub CreateCounters()
      ' Create the counter.
      PC = New PerformanceCounter("ElapsedTimeSampleCategory", _
            "ElapsedTimeSample", False)
   End Sub 'CreateCounters
   
   
   
   Private Shared Sub CollectSamples(samplesList As ArrayList)
      
      Dim pcValue As Long
      Dim Start As DateTime
      
      ' Initialize the counter.
      QueryPerformanceCounter(pcValue)
      PC.RawValue = pcValue
      Start = DateTime.Now
      
      ' Loop for the samples.
      Dim j As Integer
      For j = 0 To 999
         ' 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())
            samplesList.Add(PC.NextSample())
         End If
         
         ' reset the counter on 100th iteration.
         If j Mod 100 = 0 Then
            QueryPerformanceCounter(pcValue)
            PC.RawValue = pcValue
            Start = DateTime.Now
         End If
         System.Threading.Thread.Sleep(50)
      Next j
      
      Console.WriteLine(("Elapsed time = " + _
            DateTime.Now.Subtract(Start).ToString()))
   End Sub 
   
   
   Private Shared Sub OutputSample(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
   
   ' Reads the counter information to enable setting the RawValue.
   <DllImport("Kernel32.dll")>  _
   Public Shared Function _
        QueryPerformanceCounter(ByRef value As Long) As Boolean
   End Function
End Class

[C#] 
using System;
using System.Collections;
using System.Collections.Specialized;
using System.Diagnostics;
using System.Runtime.InteropServices;

public class App 
{
    private static PerformanceCounter PC;


    public static void Main()
    {    
        ArrayList samplesList = new ArrayList();

        SetupCategory();
        CreateCounters();
        CollectSamples(samplesList);
    }


    private static bool SetupCategory()
    {
        if ( !PerformanceCounterCategory.Exists("ElapsedTimeSampleCategory") ) 
        {

            CounterCreationDataCollection CCDC = new CounterCreationDataCollection();

            // Add the counter.
            CounterCreationData ETimeData = new CounterCreationData();
            ETimeData.CounterType = PerformanceCounterType.ElapsedTime;
            ETimeData.CounterName = "ElapsedTimeSample";
            CCDC.Add(ETimeData);       
        
            // Create the category.
            PerformanceCounterCategory.Create("ElapsedTimeSampleCategory", 
                "Demonstrates usage of the ElapsedTime performance counter type.",    
                CCDC);

            return(true);
        }
        else
        {
            Console.WriteLine("Category exists - ElapsedTimeSampleCategory");
            return(false);
        }
    }

    private static void CreateCounters()
    {
        // Create the counter.
        PC = new PerformanceCounter("ElapsedTimeSampleCategory", 
            "ElapsedTimeSample", 
            false);
  
    }

    private static void CollectSamples(ArrayList samplesList)
    {
    
        long pcValue;
        DateTime Start;

        // Initialize the counter.
        QueryPerformanceCounter(out pcValue);
        PC.RawValue = pcValue;
        Start = DateTime.Now;

        // Loop for the samples.
        for (int j = 0; j < 1000; 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());
                samplesList.Add( PC.NextSample() );
            }

            // Reset the counter on 100th iteration.
            if (j % 100 == 0)
            {
                QueryPerformanceCounter(out pcValue);
                PC.RawValue = pcValue;
                Start = DateTime.Now;
            }
            System.Threading.Thread.Sleep(50);
        }

        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("++++++++++++++++++++++");
    }


    // Reads the counter information to enable setting the RawValue.
    [DllImport("Kernel32.dll")]
    public static extern bool QueryPerformanceCounter(out long value);
}

[C++] 
#using <mscorlib.dll>
#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(S"\r\n+++++++++++");
    Console::WriteLine(S"Sample values - \r\n");
    Console::WriteLine(S"   BaseValue        = {0}", __box(s.BaseValue));
    Console::WriteLine(S"   CounterFrequency = {0}", __box(s.CounterFrequency));
    Console::WriteLine(S"   CounterTimeStamp = {0}", __box(s.CounterTimeStamp));
    Console::WriteLine(S"   CounterType      = {0}", __box(s.CounterType));
    Console::WriteLine(S"   RawValue         = {0}", __box(s.RawValue));
    Console::WriteLine(S"   SystemFrequency  = {0}", __box(s.SystemFrequency));
    Console::WriteLine(S"   TimeStamp        = {0}", __box(s.TimeStamp));
    Console::WriteLine(S"   TimeStamp100nSec = {0}", __box(s.TimeStamp100nSec));
    Console::WriteLine(S"++++++++++++++++++++++");
}


// Reads the counter information to enable setting the RawValue.
[DllImport(S"Kernel32.dll")]
extern bool QueryPerformanceCounter([Out] __int64* value);

bool SetupCategory() {
    if (!PerformanceCounterCategory::Exists(S"ElapsedTimeSampleCategory")) {

        CounterCreationDataCollection* CCDC = new CounterCreationDataCollection();

        // Add the counter.
        CounterCreationData* ETimeData = new CounterCreationData();
        ETimeData->CounterType = PerformanceCounterType::ElapsedTime;
        ETimeData->CounterName = S"ElapsedTimeSample";
        CCDC->Add(ETimeData);            

        // Create the category.
        PerformanceCounterCategory::Create(S"ElapsedTimeSampleCategory", 
            S"Demonstrates usage of the ElapsedTime performance counter type.",    
            CCDC);
        return true;
    } else {
        Console::WriteLine(S"Category exists - ElapsedTimeSampleCategory");
        return false;
    }
}

void CreateCounters(PerformanceCounter*& PC) {
    // Create the counter.
    PC = new PerformanceCounter(S"ElapsedTimeSampleCategory", 
        S"ElapsedTimeSample", false);
}

void CollectSamples(ArrayList* samplesList, PerformanceCounter* PC) {
    __int64 pcValue;
    DateTime Start;

    // Initialize the counter.
    QueryPerformanceCounter(&pcValue);
    PC->RawValue = pcValue;
    Start = DateTime::Now;

    // Loop for the samples.
    for (int j = 0; j < 1000; j++) {
        // Output the values.
        if ((j % 10) == 9) {
            Console::WriteLine(S"NextValue() = {0}", __box(PC->NextValue()));
            Console::WriteLine(S"Actual elapsed time = {0}", __box(DateTime::Now.Subtract(Start)));
            OutputSample(PC->NextSample());
            samplesList->Add(__box(PC->NextSample()));
        }

        // reset the counter on 100th iteration.
        if (j % 100 == 0) {
            QueryPerformanceCounter(&pcValue);
            PC->RawValue = pcValue;
            Start = DateTime::Now;
        }
        System::Threading::Thread::Sleep(50);
    }

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

void main() {    
    ArrayList* samplesList = new ArrayList();
    PerformanceCounter* PC;

    SetupCategory();
    CreateCounters(PC);
    CollectSamples(samplesList, PC);
}

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows NT Server 4.0, Windows NT Workstation 4.0, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

See Also

PerformanceCounter Class | PerformanceCounter Members | System.Diagnostics Namespace

Show:
© 2015 Microsoft