Export (0) Print
Expand All

Walkthrough: Changing and Retrieving Performance Counter Values 

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 property to both set and retrieve the raw value of the counter.

  • Write code using the Increment, IncrementBy, and M:System.Diagnostics.PerformanceCounter.Decrement methods to alter the value of the counter and retrieve its new value.

NoteNote

The dialog boxes and menu commands you see might differ from those described in Help depending on your active settings or edition. To change your settings, choose Import and Export Settings on the Tools menu. For more information, see Visual Studio Settings.

To create your Windows Application

  1. On the File menu, point to New, then click Project.

  2. In the New Project dialog box, create a Visual Basic, Visual C#, or Visual J# Windows Application. Name the project PerformanceCounterExample.

  3. From the Windows Forms tab of the Toolbox, add the following controls to your application:

    • Two labels

    • One text box

    • Five buttons

  4. Set the following properties on your controls:

    Control Property Value

    Label1

    Name

    lblCounterValue

     

    Text

    (blank)

    Label2

    Name

    lblSystemCounterValue

     

    Text

    (blank)

    Textbox1

    Name

    txtValue

     

    Text

    (blank)

    Button1

    Name

    btnSetRawValue

     

    Text

    Set Raw Value of the Custom Counter

    Button2

    Name

    btnGetNextValue

     

    Text

    Get Next Value of System Counter

    Button3

    Name

    btnIncrement

     

    Text

    Increase the Custom Counter by 1

    Button4

    Name

    btnDecrement

     

    Text

    Decrease the Custom Counter by 1

    Button5

    Name

    btnIncrementBy

     

    Text

    Increase 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 How to: Access and Initialize Server Explorer/Database 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# and Visual J#.

To retrieve the raw value of the system counter

  1. In the Visual J# version, on the View menu, click Code to access the Code Editor for Form1.

  2. In the designer, double-click the Get Next Value of System Counter 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:

    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
    
    

    private void btnGetNextValue_Click(object sender, System.EventArgs e)
    {
       lblSystemCounterValue.Text = 
          "The current value of the system counter is: " 
          + performanceCounter1.NextValue().ToString();
    }
    
    

    private void btnGetNextValue_Click (Object sender, System.EventArgs e)
    {
       lblSystemCounterValue.set_Text("The current value of the system counter is: " + performanceCounter1.NextValue());
    }
    

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 How to: Access and Initialize Server Explorer/Database 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.

    NoteNote

    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.

    NoteNote

    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. (If this command is not available to you, right-click the Properties window and choose Commands on the shortcut menu.) 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# and Visual J#.

  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.

    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
    
    

    private void btnSetRawValue_Click(object sender, System.EventArgs e)
    {
       performanceCounter2.RawValue = long.Parse(txtValue.Text);
       lblCounterValue.Text = performanceCounter2.NextValue().ToString();
    }
    
    

    private void btnSetRawValue_Click (Object sender, System.EventArgs e)
    {
       performanceCounter2.set_RawValue(Long.parseLong(txtValue.get_Text()));
       lblCounterValue.set_Text("" + performanceCounter2.NextValue());
    }
    

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.

    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
    
    

    private void btnIncrement_Click(object sender, System.EventArgs e)
    {
       performanceCounter2.Increment();
       lblCounterValue.Text = performanceCounter2.NextValue().ToString();
    }
    
    

    private void btnIncrement_Click (Object sender, System.EventArgs e)
    {
       performanceCounter2.Increment();
       lblCounterValue.set_Text("" + performanceCounter2.NextValue());
    }
    

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.

    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
    
    

    private void btnDecrement_Click(object sender, System.EventArgs e)
    {
       performanceCounter2.Decrement();
       lblCounterValue.Text = performanceCounter2.NextValue().ToString();
    }
    
    

    private void btnDecrement_Click (Object sender, System.EventArgs e)
    {
       performanceCounter2.Decrement();
       lblCounterValue.set_Text("" + performanceCounter2.NextValue());
    }
    

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.

    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
    
    

    private void btnIncrementBy_Click(object sender, System.EventArgs e)
    {
       performanceCounter2.IncrementBy(long.Parse(txtValue.Text));
       lblCounterValue.Text = performanceCounter2.NextValue().ToString();
    }
    
    
    

    private void btnIncrementBy_Click (Object sender, System.EventArgs e)
    {
       performanceCounter2.IncrementBy(Long.parseLong(txtValue.get_Text()));
       lblCounterValue.set_Text("" + performanceCounter2.NextValue());
    }
    

To test your application

  1. Save your files.

  2. Press F5 to compile and start your 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. Open the Performance tool that is part of the Microsoft Management Console. Consult the operating system Help to locate the steps for opening the Performance tool.

  2. Right-click the counter list below the performance graph, and select Add Counters from the shortcut menu.

  3. Select MyNewCategory from the Performance object list and MyNewCounter from the counter list. Click Add to finish.

  4. Click View Report from the toolbar to display the value of your counter.

Community Additions

ADD
Show:
© 2014 Microsoft