Bagikan melalui


Walkthrough: Retrieving Categories and Counters

The procedures in this walkthrough take you through the process of creating and configuring PerformanceCounter component instances and using them to retrieve lists of performance counter categories and counters on your system. A performance counter is the means by which Windows collects performance data on various system resources. Windows contains a set of predefined counters, organized into categories, with which you can interact. Each category and counter is related to a specific area of system functionality.

In this walkthrough you will:

  • Instantiate a PerformanceCounter component and configure it to interact with a specific category of system-generated counters.

  • Create a Windows application that displays information about categories and counters in a list box.

  • Use the GetCategories method to return a list of categories on the local computer.

  • Use the GetCounters method to return a list of counters from the specified category.

To create your Windows application

  1. From the New Project dialog box, create a Visual Basic or Visual C# Windows Application.

  2. From the Windows Forms tab of the Toolbox, add two buttons and two list boxes to your form. Arrange them in any order you like, and then set the following properties for them:

    Control

    Property

    Value

    Button1

    Name

    btnGetCounters

     

    Text

    Get Counters

    Button2

    Name

    btnGetCategories

     

    Text

    Get Categories

    ListBox1

    Name

    lstCounters

     

    ScrollAlwaysVisible

    True

    ListBox2

    Name

    lstCategories

     

    ScrollAlwaysVisible

    True

  3. Save your work.

To retrieve the category list

  1. In Design view, double-click the Get Categories button to access the Code Editor.

  2. In the btnGetCategories_Click procedure, add the following code to retrieve the list of categories from the local computer.

    Private Sub btnGetCategories_Click(ByVal sender As System.Object, _
       ByVal e As System.EventArgs) Handles btnGetCategories.Click
    
       Dim myCat2 As PerformanceCounterCategory()
       Dim i As Integer 
       ' Remove the current contents of the list. 
       Me.lstCategories.Items.Clear()
       ' Retrieve the categories.
       myCat2 = PerformanceCounterCategory.GetCategories
       ' Add the retrieved categories to the list. 
       For i = 0 To myCat2.Length - 1
          Me.lstCategories.Items.Add(myCat2(i).CategoryName)
       Next 
    End Sub
    
    private void btnGetCategories_Click(object sender, System.EventArgs e)
    {
       System.Diagnostics.PerformanceCounterCategory[] myCat2;
       // Remove the current contents of the list.
       this.lstCategories.Items.Clear();
       // Retrieve the categories.
       myCat2 = 
          System.Diagnostics.PerformanceCounterCategory.GetCategories();
       // Add the retrieved categories to the list.
       for (int i = 0; i < myCat2.Length; i+) 
       {
          this.lstCategories.Items.Add(myCat2[i].CategoryName);
       }
    }
    

To retrieve the counter list

  1. In Design view, double-click the Get Counters button to access the Code Editor. The insertion point will be positioned in the btnGetCounters_Click event.

  2. Add the following code to retrieve the list of counters from the selected category.

    Private Sub btnGetCounters_Click(ByVal sender As System.Object, _
       ByVal e As System.EventArgs) Handles btnGetCounters.Click
    
       Dim instanceNames() As String 
       Dim counters As New System.Collections.ArrayList()
    
       If (Me.lstCategories.SelectedIndex <> -1) Then 
          Dim mycat As New PerformanceCounterCategory( _
             Me.lstCategories.SelectedItem.ToString())
          ' Remove the current contents of the list. 
          Me.lstCounters.Items.Clear()
          ' Retrieve the counters. 
          Try
             instanceNames = mycat.GetInstanceNames()
             If (instanceNames.Length = 0) Then
                counters.AddRange(mycat.GetCounters())
             Else 
                Dim i As Integer 
                For i = 0 To instanceNames.Length - 1
                   counters.AddRange( _
                      mycat.GetCounters(instanceNames(i)))
                Next 
             End If 
             ' Add the retrieved counters to the list. 
             Dim counter As PerformanceCounter
             For Each counter In counters
                Me.lstCounters.Items.Add(counter.CounterName)
             Next 
          Catch ex As Exception
             MessageBox.Show( _
                "Unable to list the counters for this category:" _
                & ControlChars.CrLf & ex.Message)
          End Try 
       End If 
    End Sub
    
    private void btnGetCounters_Click(object sender, System.EventArgs e)
    {
       string[] instanceNames;
       System.Collections.ArrayList counters = new 
          System.Collections.ArrayList();
       if (this.lstCategories.SelectedIndex != -1) 
       {
          System.Diagnostics.PerformanceCounterCategory mycat = new 
             System.Diagnostics.PerformanceCounterCategory(
             this.lstCategories.SelectedItem.ToString());
          // Remove the current contents of the list.
          this.lstCounters.Items.Clear();
          // Retrieve the counters.
          try 
          {
             instanceNames = mycat.GetInstanceNames();
             if (instanceNames.Length == 0) 
             {
                counters.AddRange(mycat.GetCounters());
             }
             else
             {
                for (int i = 0; i < instanceNames.Length; i+)
                {
                   counters.AddRange(mycat.GetCounters(instanceNames[i]));
                }
             }
    
             // Add the retrieved counters to the list.
             foreach (System.Diagnostics.PerformanceCounter counter 
                in counters) 
             {
                this.lstCounters.Items.Add(counter.CounterName);
             }
          }
          catch (System.Exception ex)
          {
             MessageBox.Show(
                "Unable to list the counters for this category:\n" 
                + ex.Message);
          }
       }
    }
    

To test your application

  1. Save and compile your application.

  2. Click the Get Categories button. You will see a list of categories in the list box.

  3. Select one of the categories and click the Get Counters button. You will see a list of the counters for the selected category.

See Also

Concepts

Introduction to Monitoring Performance Thresholds

Other Resources

Monitoring Performance Thresholds

Performance Counter Walkthroughs