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:
- 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#
|
- Reading the NextValue property of the counter:
Remember that you have to read NextValue more than once to get a relevant data.
Example
C# PCRead.exe
[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.
|