Home   |   QuickStart Welcome   |   ASP.NET   |   Web Services   |   How Do I...?   
  |   I want my samples in...      

How Do I...? Common Tasks QuickStart Tutorial

How Do I...Read from a performance counter?



Windows performance counters enable your applications and components to publish, capture (read), and analyze the performance data that applications, services, and drivers provide. You can use this information to determine system bottlenecks and fine-tune system and application performance. For example, you can use a performance counter to track the number of orders processed per second or a system's processor utilization. Using the common language runtime's PerformanceCounter component, you can easily read performance data relevant to your application, such as those mentioned above.

This sample illustrates how to read simple performance information from a performance counter. It is a small console application that can be run from a command prompt. The application takes three command line arguments. The first is a performance object name (category). The second argument is the counter name. The third argument is the counter instance name.

For example, in order to see the processor utilization on your machine run the sample with the following command line arguments:
> PCRead.exe "Processor" "% Processor Time" "_Total"


You will see the processor utilization data updated every half-second.

In its simplest form, reading a performance counter involves:
  1. Creating a new instance of a PerformanceCounter component and pointing it to an appropriate performance counter:

    		
    String objectName = ... ;
    String counterName = ... ;
    String instanceName = ... ;
    
    PerformanceCounter counter;
    counter = new PerformanceCounter(objectName, counterName, instanceName);
    
    C#


  2. Reading the NextValue property of the counter:

    		
    counter.NextValue();
    
    C#


Remember that you have to read NextValue more than once to get a relevant data.

Example

VB PCRead.exe
View Source
[This sample can be found at C:\DevFusion.Data\legacy\quickstart.developerfusion.co.uk\QuickStart\howto\samples\Services\PerformanceCounters\PCRead\]



Microsoft .NET Framework SDK QuickStart Tutorials Version 2.0
Copyright � 2004 Microsoft Corporation. All rights reserved.