Performance Counters Sample
This sample shows how to read from and write to performance counters. These counters can be used to track the performance of the operating system and .NET Framework applications. Both system and custom counters are used.
To get samples and instructions for installing them
Do one or more of the following:
On the Help menu, click Samples.
The Readme displays information about samples.
Visit the Visual Studio 2008 Samples Web site. The most recent versions of samples are available there.
Locate samples on the computer on which Visual Studio is installed. By default, samples and a Readme file are installed in drive:\Program Files\Microsoft Visual Studio 9.0\Samples\lcid. For Express editions of Visual Studio, all samples are located online.
For more information, see Visual Studio Samples.
This sample code is intended to illustrate a concept, and it shows only the code that is relevant to that concept. It may not meet the security requirements for a specific environment, and it should not be used exactly as shown. We recommend that you add security and error-handling code to make your projects more secure and robust. Microsoft provides this sample code "AS IS" with no warranties.
To run this sample
Create a custom performance counter that can be written to.
Open Server Explorer.
Open the Servers node, open the server node for your computer, and then open the Performance Counters node.
Right-click Performance Counters and click Create New Category.
In the Performance Counter Builder Dialog Box, type Performance Counter Sample as the Category Name, and any description in the Category Description text box.
Click the New button to add a new performance counter.
Type a name and description for the counter. Choose NumberOfItems32 for the Type.
Press F5 to run the project.
Choose the custom performance counter you built earlier. Choose it by Category and Counter, and then use the buttons to increment and decrement the value.
Tasks demonstrated include the following:
Retrieving data The NextValue method is used to retrieve the current value of the selected counter.
Custom counters The application only allows you to increment custom counters. A custom counter is defined as a counter where you cannot call the NextValue method.