Performance Counters Sample
The document is archived and information here might be outdated

Performance Counters Sample 

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

Security noteSecurity Note

This sample code is provided to illustrate a concept and should not be used in applications or Web sites, as it may not illustrate the safest coding practices. Microsoft assumes no liability for incidental or consequential damages should the sample code be used for purposes other than as intended.

To open the sample file in Solution Explorer

  1. Click Download Sample.

    The File Download message box appears.

  2. Click Open, and on the left column of the zip folder window, click Extract all files.

    The Extraction Wizard opens.

  3. Click Next. You can change the directory that the files will be extracted to, and then click Next again.

  4. Make sure that the Show extracted files check box is selected, and click Finish.

  5. Double-click the sample's .sln file.

    The sample solution is displayed in Solution Explorer. You might get a security warning that says the solution location is not trusted. Click OK to continue.

To run this sample

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

    1. Open Server Explorer.

    2. Open the Servers node, then 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.


Tasks demonstrated include:

  • 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

© 2016 Microsoft