PerformanceCounter::NextValue Method
Obtains a counter sample and returns the calculated value for it.
Assembly: System (in System.dll)
| Exception | Condition |
|---|---|
| InvalidOperationException | The instance is not correctly associated with a performance counter. |
| Win32Exception | An error occurred when accessing a system API. |
| PlatformNotSupportedException | The platform is Windows 98 or Windows Millennium Edition (Me), which does not support performance counters. |
| UnauthorizedAccessException | Code that is executing without administrative privileges attempted to read a performance counter. |
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 |
|---|
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.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; // Add the counter. CounterCreationData^ ETimeData = gcnew CounterCreationData; ETimeData->CounterType = PerformanceCounterType::ElapsedTime; ETimeData->CounterName = counterName; CCDC->Add( ETimeData ); // 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; } System::Threading::Thread::Sleep( 50 ); } Console::WriteLine( "Elapsed time = {0}", DateTime::Now.Subtract( Start ) ); } int main() { CollectSamples(); }
- PerformanceCounterPermission
for reading the performance counter category. Associated enumeration: PerformanceCounterPermissionAccess::Read.
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.
Note