Udostępnij za pośrednictwem


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.

Security noteSecurity Note:

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

  1. Create a custom performance counter that can be written to.

    1. Open Server Explorer.

    2. Open the Servers node, open the server node for your computer, and then open the Performance Counters node.

    3. Right-click Performance Counters and click Create New Category.

    4. In the Performance Counter Builder Dialog Box, type Performance Counter Sample as the Category Name, and any description in the Category Description text box.

    5. Click the New button to add a new performance counter.

    6. Type a name and description for the counter. Choose NumberOfItems32 for the Type.

  2. Press F5 to run the project.

  3. 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.

Demonstrates

Tasks demonstrated include the following:

  • Listing categories   Performance counter categories are retrieved using the GetCategories method and displayed in a ComboBox control.

  • Listing counters   Performance counters are retrieved using the GetCounters method and displayed in a ComboBox control. Only the counters from the selected category are retrieved.

  • 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.

See Also

Concepts

Introduction to Monitoring Performance Thresholds

Introduction to Monitoring Performance Thresholds

Reference

PerformanceCounter

System.Diagnostics