
.NET 1.1+Setting Performance Counters (2)
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.
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:
using System.Diagnostics;
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' code block:
PerformanceCounter _counter;
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)
{
_counter.Dispose();
}
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