Setting Performance Counters
Microsoft Windows provides a large number of performance counters that can be monitored with tools such as Performance Monitor. It is possible to add new counters and set their values to allow such tools to monitor custom .NET applications and services.
Microsoft Windows provides a number of counters that can be used to determine how well the operating system, hardware, services and drivers are performing. You can also create your own performance counters and categories and monitor these using tools such as Performance Monitor. This can be useful in determining the correct operation of your software and to assist in identifying performance bottlenecks, bugs or memory leaks.
Creating a New Performance Counter
In this article we will a new single-instance performance counter and a sample application that allows the counter's value to be changed. We will view the changes with Performance Monitor. We will create the counter manually using the tools provided by Visual Studio. You can also create performance counters programmatically, though this will not be examined here. To begin, open Visual Studio as an administrative user. This is required to allow a performance counter to be added.
Performance counter categories and their contained counters can be viewed in the Server Explorer pane. To view this, select "Server Explorer" from the View menu. Within the Server Explorer, expand the Servers node and the name of your computer. You should see a branch of the tree named, "Performance Counters". If you expand this section you will see a list of all of the categories that are present on the computer. Within each of these nodes will be the names of the contained counters.
To create a new performance counter category, right-click the Performance Counters tree node. Select "Create New Category..." from the context-sensitive menu that appears. This displays the Performance Counter Builder dialog box, pictured below:
The image above shows the performance counter that we will now create. First, enter "Custom Counters" as the category name. This would normally contain the name of the application or service that you wish to be monitored. Add a suitable description for the category.
To add a counter to the category, click the New button. You can specify a name, type and description for each counter that you include in the category. For this demonstration, name the counter, "Example Counter". The counter will present a single integer value so select "NumberOfItems32" as the type. Add a description and click the OK button to create the category and counter. You should be able to see the new items in the Server Explorer.
Viewing the Performance Counter
To monitor the counter, launch the Performance Monitor software and add the new counter to the chart. You should see that the new chart line is constantly set to zero, as no value has been provided yet. Leave the performance monitor running.
Setting Performance Counter Values
The .NET framework provides a class that allows you to set performance counter values. The examples in this article use the PerformanceCounter class in the System.Diagnostics namespace. This class can be used both to read performance counters and to publish information for custom counters.
Creating the Test Application
The test application will be a simple Windows Forms application. The single form will include a NumericUpDown control that, when changed, sets the value of the example performance counter. The change will be seen in the Performance Monitor trace. To begin, create a new Windows Forms project. Add a NumericUpDown control to the initial form.
As the PerformanceCounter class is found in the System.Diagnostics namespace, add the following using directive to the form's code:
Attaching the Counters
To modify a counter value, a PerformanceCounter instance is required. This must be configured with the details of the performance counter category and the counter name. The counter will be accessed from several methods within the form's class so should be created as a class-level variable. To create this variable, add the following declaration within the class's code block:
The counter object will be initialised when the form is loaded. To achieve this, double-click the form to add the Load event and add the code shown below. This initialises the counter variable by specifying the names of the category and the counter to be attached. The Boolean value provided to the final parameter specifies that the counter will not be read-only.
NB: If you are using a counter that includes multiple instances, use the overloaded version of the constructor that permits the name of the instance to be specified.
private void Form1_Load(object sender, EventArgs e)
_counter = new PerformanceCounter("Custom Counters", "Example Counter", false);
The PerformanceCounter class implements the IDisposable interface. As such, to ensure that any unmanaged resources are correctly freed, the Dispose method should be called when the object is no longer required. For simplicity, this can be added to the FormClosed event. In real applications you should consider carefully where the call to the Dispose method appears.
private void Form1_FormClosed(object sender, FormClosedEventArgs e)
Setting Single Instance Counter Values
The final task is to allow the performance counter value to be set. This will happen whenever the value in the numeric control is changed and the ValueChanged event is triggered. To modify the counter value we can set the RawValue property. This expects a 64-bit integer value so the value of the input control is cast as a long. Add the event and include the following code:
private void numericUpDown1_ValueChanged(object sender, EventArgs e)
_counter.RawValue = (long)numericUpDown1.Value;
You can now execute the program and try changing the value in the NumericUpDown control. The new value should be seen in Performance Monitor as the chart refreshes.
3 August 2010