Export (0) Print
Expand All

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.

NoteNote:

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.

NoteNote:

There are security restrictions that affect your ability to use performance counters. For more information, see Introduction to Monitoring Performance Thresholds.

NoteNote:

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 How to: Create PerformanceCounter Component Instances or How to: Configure PerformanceCounter Component Instances.

  2. Write the value using one of the following methods:

    To

    Call this method

    Parameter

    Increase the raw value by one

    Increment

    None

    Decrease the raw value by one

    Decrement

    None

    Increase the raw value by greater than one

    IncrementBy

    A positive integer

    Decrease the raw value by greater than one

    IncrementBy

    A negative integer

    Reset the raw value to any integer, rather than incrementing it

    RawValue

    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.

    	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);
    	}
    

Community Additions

ADD
Show:
© 2015 Microsoft