Export (0) Print
Expand All

Walkthrough: Changing and Retrieving Performance Counter Values

Note   The Servers node of Server Explorer used in this walkthrough is not available in the Standard Edition of Visual Basic and Visual C# .NET. For more information, see Visual Basic Standard Edition Features or Visual C# Standard Edition Features.

The procedures in this walkthrough show you how to work with the values of a performance counter using the methods on the PerformanceCounter class. A performance counter is the means by which Windows collects performance data on various system resources. Windows contains a set of pre-defined counters, organized into categories, with which you can interact. The values you retrieve from a counter can either be raw values or calculated values that alter over time. There are several ways to increase and decrease the current value of a counter.

In the course of this walkthrough, you will:

  • Create and configure a PerformanceCounter component to interact with a system-provided counter.
  • Create a Windows application that contains UI elements that retrieve and display values from a counter.
  • Write code using the RawValue method to both set and retrieve the raw value of the counter.
  • Write code using the Increment, IncrementBy, and Decrement methods to alter the value of the counter and retrieve its new value.

To create your Windows Application

  1. On the File menu, point to New, then click Project.
  2. In the New Project dialog box, choose Visual Basic or Visual C# in the left pane, then choose the Windows Application template. Name the project PerformanceCounterExample.
  3. From the Windows Forms tab of the Toolbox, add the following controls to your application:
    • Two labels
    • A text box
    • Five buttons
  4. Set the following properties on your controls:
    ControlPropertyValue
    Label1NamelblCounterValue
     Text(blank)
    Label2NamelblSystemCounterValue
     Text(blank)
    Textbox1NametxtValue
     Text(blank)
    Button1NamebtnSetRawValue
     TextSet Raw Value of the Custom Counter
    Button2NamebtnGetNextValue
     TextGet Next Value of System Counter
    Button3NamebtnIncrement
     TextIncrease the Custom Counter by 1
    Button4NamebtnDecrement
     TextDecrease the Custom Counter by 1
    Button5NamebtnIncrementBy
     TextIncrease Custom Counter by a value
  5. Arrange the controls as you like.
  6. Save your work.

To create and configure your PerformanceCounter component

  1. Open Server Explorer and access the Servers node. For more information, see Accessing and Initializing Server Explorer.
  2. Locate the listing for your computer under the Servers node and expand it. You will see entries for performance counters, messages queues, event logs, and services.
  3. Expand the Performance Counters node and locate the Processor node.
  4. Locate the % Processor Time node and expand it.
  5. Drag the _Total counter onto the form. A PerformanceCounter component configured for the _Total counter is added to your project. The component is named PerformanceCounter1 in Visual Basic, and performanceCounter1 in Visual C#.

To retrieve the raw value of the system counter

  1. In the designer, double-click the Get Next Value button to create the Click event handler. This method will report the calculated value of the counter, not the raw value. Add the following code to retrieve and display the next value of the counter your PerformanceCounter instance is watching:
    ' Visual Basic
    Private Sub btnGetNextValue_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles btnGetNextValue.Click
       lblSystemCounterValue.Text = _
          "The current value of the system counter is: " _
          & PerformanceCounter1.NextValue().ToString()
    End Sub
    
    // C#
    private void btnGetNextValue_Click(object sender, System.EventArgs e)
    {
       lblSystemCounterValue.Text = 
          "The current value of the system counter is: " 
          + performanceCounter1.NextValue().ToString();
    }
    

For the rest of your procedures, you will work with a custom category and counter.

To create a custom counter and category

  1. On the View menu, click Designer to access the designer for Form1.
  2. In the designer, open Server Explorer and access the Servers node. For more information, see Accessing and Initializing Server Explorer.
  3. Locate the listing for your computer under the Servers node and expand it.
  4. Right-click the Performance Counters node and select Create New Category from the shortcut menu.

    The Performance Counter Builder dialog box appears.

  5. Enter MyNewCategory for the Category name.
  6. Click New to add a new counter, and name it MyNewCounter. Click OK to create the new category and counter.
    Note   You will need permission to write to the registry for this step. Contact your system administrator if you are unable to create the new counter.
  7. In Server Explorer, browse to the new counter and drag it onto the form. A new PerformanceCounter component is added to the project, configured for the new counter, MyNewCounter.
    Note   These steps create the counter on your computer. If you were to deploy this application, you would need to create the counter on the target computer. You can do this by adding an Installer to the project. Select the PerformanceCounter object, and click Add Installer in the Properties window. For more information, see Introduction to Installation Components.

To set the raw value of the custom counter

  1. In the designer, select the PerformanceCounter2 in Visual Basic, or performanceCounter2 in Visual C#.
  2. Set the ReadOnly property to false.
  3. Double-click the btnSetRawValue button to create the Click event handler in the Code Editor.
  4. Add the following code to set the raw value of the counter you created and to display the counter's value in the first of the label controls.
    ' Visual Basic
    Private Sub btnSetRawValue_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles btnSetRawValue.Click
       PerformanceCounter2.RawValue = CLng(txtValue.Text)
       lblCounterValue.Text = PerformanceCounter2.NextValue().ToString()
    End Sub
    
    // C#
    private void btnSetRawValue_Click(object sender, System.EventArgs e)
    {
       performanceCounter2.RawValue = long.Parse(txtValue.Text);
       lblCounterValue.Text = performanceCounter2.NextValue().ToString();
    }
    

To increase the custom counter value by one and display it

  1. In the designer, double-click the Increase the Custom Counter by 1 button to create the Click event handler in the Code Editor.
  2. Add the following code to add one to your custom counter and to display the counter's value in the first of the label controls.
    ' Visual Basic
    Private Sub btnIncrement_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles btnIncrement.Click
       PerformanceCounter2.Increment()
       lblCounterValue.Text = PerformanceCounter2.NextValue().ToString()
    End Sub
    
    // C#
    private void btnIncrement_Click(object sender, System.EventArgs e)
    {
       performanceCounter2.Increment();
       lblCounterValue.Text = performanceCounter2.NextValue().ToString();
    }
    

To decrease the custom counter and display its value

  1. In the designer, double-click the Decrease the Custom Counter by 1 button to create the Click event handler in the Code Editor.
  2. Add the following code to decrease the counter value by one and to display the counter's value in the first of the label controls.
    ' Visual Basic
    Private Sub btnDecrement_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles btnDecrement.Click
       PerformanceCounter2.Decrement()
       lblCounterValue.Text = PerformanceCounter2.NextValue().ToString()
    End Sub
    
    // C#
    private void btnDecrement_Click(object sender, System.EventArgs e)
    {
       performanceCounter2.Decrement();
       lblCounterValue.Text = performanceCounter2.NextValue().ToString();
    }
    

To increase the custom counter by a user-defined value

  1. In the designer, double-click the Increase Custom Counter by a value button to create the Click event handler in the Code Editor.
  2. Add the following code to increase the counter value by the value entered in the text box and to display the counter's value in the first of the label controls.
    ' Visual Basic
    Private Sub btnIncrementBy_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles btnIncrementBy.Click
       PerformanceCounter2.IncrementBy(CLng(txtValue.Text))
       lblCounterValue.Text = PerformanceCounter2.NextValue().ToString()
    End Sub
    
    // C#
    private void btnIncrementBy_Click(object sender, System.EventArgs e)
    {
       performanceCounter2.IncrementBy(long.Parse(txtValue.Text));
       lblCounterValue.Text = performanceCounter2.NextValue().ToString();
    }
    

To test your application

  1. Save your files.
  2. Run the application.
  3. Click the Get Next Value of System Counter button to retrieve the current value of the Processor category's counter. Since you are retrieving the value using the NextValue() method, the first call will return 0.

    You should see the current value displayed in the label.

  4. Type 25 in the text box and click Set Raw Value of Custom Counter.

    The label field should be updated to indicate that the raw value is now 25.

  5. Click the Increase Custom Counter by 1 button.

    The value in the label should increase by 1.

  6. Click the Decrease Custom Counter by 1 button.

    The value in the label should decrease by 1.

  7. Enter 25 in the text box and click Increase Custom Counter by a value.

    The value in the label should increase by 25.

    You can also view your performance counter in the Windows Performance tool.

To view your performance counter in the Windows Performance tool

  1. On the taskbar, click the Start button, point to Settings, and then click Control Panel.
  2. In Control Panel, double-click Administrative Tools, and then double-click Performance.
  3. Right-click the counter list below the performance graph, and select Add Counters from the shortcut menu.
  4. Select MyNewCategory from the Performance object list and MyNewCounter from the counter list. Click Add to finish.
  5. Click View Report from the toolbar to display the value of your counter.

See Also

Monitoring Performance Thresholds | Introduction to Monitoring Performance Thresholds | Performance Counter Walkthroughs

Show:
© 2014 Microsoft