Welcome   |  ASP.NET   |  Web Services   |  How Do I...?   |  Class Browser  | WPF Quick Starts
  |   I want my samples in...   

How Do I...? Common Tasks QuickStart Tutorial

Go To...

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

C# PCRead.exe
View Source

[This sample can be found at M:\web\users\sites\AspnetQuickStart\v2.0\QuickStart\howto\samples\Services\PerformanceCounters\PCRead\
To build this sample, open the SDK command prompt and navigate to the above path. Build the sample using the build tool msbuild passing the solution file as the first parameter: msbuild mySample.sln. The compiled executable will be found in the sub directory \bin directory.]




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


Hosted by MaximumASP | Found a broken link? | Contact Us | Terms and conditions | Privacy Policy | Advertise with us
� 2000 - 2010  Mindcracker LLC. All Rights Reserved