How to: Write Values to Performance Counters
You write a value to a counter by incrementing the counter's current raw value by a positive or negative number. You do this using the IncrementBy method on the PerformanceCounter class.
Note
Incrementing by a negative number decrements the counter by the absolute value of the number. For example, incrementing with a value of 3 will increase the counter's raw value by three. Incrementing with a value of –3 will decrease the counter's raw value by three.
In addition, you can use the Increment and Decrement methods to increase or decrease a counter's values by one. These methods are processed much more quickly than IncrementBy .
You can only increment values on custom counters; by default, your interactions with system counters by means of a PerformanceCounter component instance are restricted to read-only mode. Before you can increment a custom counter, you must set the ReadOnly property on the component instance with which you are accessing it to false.
Note
There are security restrictions that affect your ability to use performance counters. For more information, see Introduction to Monitoring Performance Thresholds.
Note
The PerformanceCounter class is not fully supported on Microsoft Windows NT version 4.0. You can read from the system counters, but you cannot create, write to, or delete custom counters.
To write values to performance counters
Create a PerformanceCounter instance and configure it to interact with the desired category and counter. For more information, see How to: Create PerformanceCounter Component Instances or How to: Configure PerformanceCounter Component Instances.
Write the value using one of the following methods:
To
Call this method
Parameter
Increase the raw value by one
None
Decrease the raw value by one
None
Increase the raw value by greater than one
A positive integer
Decrease the raw value by greater than one
A negative integer
Reset the raw value to any integer, rather than incrementing it
A positive or negative integer
The following code shows how to set values for a counter in various ways. This code assumes that you are working on a Windows Forms application that contains a text box named txtValue and three buttons: one that increments the raw value by the number entered in the text box, one that decrements the raw value by one, and one that sets the raw value of the counter to the value set in the text box.
Private Sub btnIncrement_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles btnIncrement.Click PerformanceCounter1.ReadOnly = False PerformanceCounter1.IncrementBy(CLng(txtValue.Text)) End Sub Private Sub btnDecrement_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles btnDecrement.Click PerformanceCounter1.ReadOnly = False PerformanceCounter1.Decrement() End Sub Private Sub btnSetValue_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles btnSetValue.Click PerformanceCounter1.ReadOnly = False PerformanceCounter1.RawValue = CLng(txtValue.Text) End Sub
protected void btnIncrement_Click(object sender, EventArgs e) { performanceCounter1.ReadOnly = false; performanceCounter1.IncrementBy(long.Parse(txtValue.Text)); } protected void btnDecrement_Click(object sender, EventArgs e) { performanceCounter1.ReadOnly = false; performanceCounter1.Decrement(); } protected void btnSetValue_Click(object sender, EventArgs e) { performanceCounter1.ReadOnly = false; performanceCounter1.RawValue = long.Parse(txtValue.Text); }