Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All

Writing 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 via 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

  1. Create a PerformanceCounter instance and configure it to interact with the desired category and counter. For more information, see Creating PerformanceCounter Component Instances or Configuring PerformanceCounter Component Instances.
  2. Write the value using one of the following methods:
    ToCall this methodParameter
    Increase the raw value by one IncrementNone
    Decrease the raw value by oneDecrementNone
    Increase the raw value by greater than oneIncrementByA positive integer
    Decrease the raw value by greater than oneIncrementByA negative integer
    Reset the raw value to any integer, rather than incrementing itRawValueA 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 Form 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.

    ' Visual Basic
    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
    
    // C#
    protected void btnIncrement_Click(System.Object sender, 
       System.EventArgs e) 
    {
       performanceCounter1.ReadOnly = false;   
       performanceCounter1.IncrementBy(long.Parse(txtValue.Text));
    }
    
    protected void btnDecrement_Click(System.Object sender,
       System.EventArgs e) 
    {
       performanceCounter1.ReadOnly = false;   
       performanceCounter1.Decrement();
    }
    
    protected void btnSetValue_Click(System.Object sender, 
       System.EventArgs e) 
    {
       performanceCounter1.ReadOnly = false;   
       performanceCounter1.RawValue = long.Parse(txtValue.Text);
    }
    

See Also

Introduction to Monitoring Performance Thresholds | Performance Counter Value Retrieval

Show:
© 2015 Microsoft