逐步解說:擷取分類和計數器

更新:2007 年 11 月

這份逐步解說中的程序,將帶領您逐一檢視如何建立和設定 PerformanceCounter 元件執行個體,並使用它們來擷取系統上效能計數器分類和計數器的清單。效能計數器是 Windows 用來在各種系統資源上收集效能資料的方法;Windows 包含一組您可與其互動的預先定義計數器 (這些計數器是以分類區分的),每個分類和計數器都與特定領域的系統功能有關。

在這份逐步解說中,您將能夠:

  • PerformanceCounter 元件個體化,並設定它來與特定分類之系統產生的計數器進行互動。

  • 建立在清單方塊中顯示分類和計數器資訊的 Windows 應用程式。

  • 使用 GetCategories 方法,傳回本機電腦 (Local Machine) 上的分類清單。

  • 使用 GetCounters 方法,從特定分類傳回計數器的清單。

若要建立您的 Windows 應用程式

  1. 在 [新增專案] 對話方塊中,建立 Visual Basic、Visual C# 或 Visual J# [Windows 應用程式]。

  2. 從 [工具箱] 的 [Windows Form] 索引標籤,將兩個按鈕和兩個清單方塊加入至您的表單。您可隨意排列它們並為其設定下列屬性:

    控制項

    屬性

    Button1

    Name

    btnGetCounters

     

    Text

    取得計數器

    Button2

    Name

    btnGetCategories

     

    Text

    取得分類

    ListBox1

    Name

    lstCounters

     

    ScrollAlwaysVisible

    True

    ListBox2

    Name

    lstCategories

     

    ScrollAlwaysVisible

    True

  3. 儲存您的工作。

若要擷取分類清單

  1. 在 [設計] 檢視中,按兩下 [取得分類] 按鈕來存取程式碼編輯器。

  2. 在 Visual J# 版中,在螢幕最上方加入參考 System.Diagnostics 命名空間的 import 陳述式。

  3. 在 btnGetCategories_Click 程序中,加入以下的程式碼,以便從本機電腦擷取分類的清單。

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

若要擷取計數器清單

  1. 在 [設計] 檢視中,按兩下 [取得計數器] 按鈕來存取程式碼編輯器。插入點會放在 btnGetCounters_Click 事件中。

  2. 加入以下程式碼,從選取的分類擷取計數器的清單。

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

若要測試您的應用程式

  1. 儲存並編譯您的應用程式。

  2. 按一下 [取得分類] 按鈕。您會在清單方塊中看到分類的清單。

  3. 選取其中一個分類,然後按一下 [取得計數器] 按鈕,您就會看到選取分類的計數器清單。

請參閱

概念

監視效能臨界值簡介

其他資源

監視效能臨界值

效能計數器逐步解說