Procedura: recuperare elenchi di contatori e categorie

Aggiornamento: novembre 2007

È possibile utilizzare il metodo GetCounters della classe PerformanceCounterCategory per recuperare rapidamente un elenco di tutti i contatori o di tutte le istanze in una categoria specifica. Ogni volta che si richiede un valore di una categoria creando un’istanza di un oggetto PerformanceCounter, impostandola su un contatore esistente e recuperando un valore, il sistema legge l’intera categoria e recupera il contatore richiesto. Ciò significa che se si richiedono cinque diversi contatori da una categoria che contiene 20 contatori, il sistema legge tutti i 20 contatori della categoria cinque volte, cercando ogni contatore separatamente. Il metodo GetCounters consente di recuperare gli stessi dati, eseguendo tuttavia una sola lettura della categoria.

Il metodo GetCounters restituisce una matrice di tipo PerformanceCounter che contiene i contatori della categoria. È possibile utilizzare la proprietà Item per il contenuto dell’insieme.

Oltre a recuperare i contatori, il metodo GetCategories statico consente di restituire un elenco di categorie nel computer corrente o in qualunque server cui si ha accesso. Il metodo GetCategories restituisce una matrice di oggetti PerformanceCounterCategory.

Per recuperare i contatori in una categoria

  1. Creare un oggetto PerformanceCounter e configurarlo in modo da puntare alla categoria desiderata. Per ulteriori informazioni, vedere Procedura: creare istanze del componente PerformanceCounter o Procedura: configurare istanze del componente PerformanceCounter.

  2. Creare una matrice di tipo PerformanceCounter in grado di contenere l’elenco di categorie risultante.

  3. Chiamare il metodo GetCounters della classe PerformanceCounterCategory e specificare la categoria desiderata come parametro.

  4. Inserire i risultati nella matrice.

    Nell'esempio riportato di seguito viene illustrato come recuperare tutti i contatori nella categoria Cache. Nel codice si presuppone che si stia utilizzando un'applicazione Windows Form che contiene un pulsante e un controllo ListBox e che sia presente un riferimento a System.dll e un'istruzione Imports (per Visual Basic) o using (per C#) per lo spazio dei nomi System.Diagnostics:

    Private Sub btnGetCounters_Click(ByVal sender As System.Object, _
       ByVal e As System.EventArgs) Handles btnGetCounters.Click
       Dim mypc() As PerformanceCounter
       Dim i As Integer
       Dim myCat As New PerformanceCounterCategory("Cache")
       ' Remove the current contents of the list.
       Me.ListBox1.Items.Clear()
       ' Retrieve the counters.
       mypc = myCat.GetCounters
       ' Add the retrieved counters to the list.
       For i = 0 To mypc.Length - 1
          Me.ListBox1.Items.Add(mypc(i).CounterName)
       Next
    End Sub
    
     private void btnGetCounters_Click(object sender, EventArgs e)
        {
            System.Diagnostics.PerformanceCounter[] mypc;
            System.Diagnostics.PerformanceCounterCategory mycat =
               new System.Diagnostics.PerformanceCounterCategory("cache");
            // Remove the current contents of the list.
            this.listBox1.Items.Clear();
            // Retrieve the counters.
            mypc = mycat.GetCounters();
            // Add the retrieved counters to the list.
            for (int i = 0; i < mypc.Length; i++)
            {
                this.listBox1.Items.Add(mypc[i].CounterName);
            }
        }
    

Per recuperare tutte le categorie in un computer

  1. Creare una matrice di tipo PerformanceCounter in grado di contenere l’elenco di categorie risultante.

  2. Chiamare il metodo GetCategories della classe PerformanceCounterCategory e specificare la categoria desiderata come parametro.

  3. Inserire i risultati nella matrice.

    Nel codice riportato di seguito viene illustrato come recuperare tutte le categorie del computer locale. Nel codice si presuppone che si stia utilizzando un'applicazione Windows Form che contiene un pulsante e un controllo ListBox e che sia presente un riferimento a System.dll e un'istruzione Imports (per Visual Basic) o using (per C#) per lo spazio dei nomi System.Diagnostics:

    Private Sub btnGetCategories_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs)
       Dim myCat2 As PerformanceCounterCategory()
       Dim i As Integer
       ' Clear the contents of the list box.
       Me.listBox2.Items.Clear()
       ' Retrieve the categories.
       myCat2 = PerformanceCounterCategory.GetCategories
       ' Add the retrieved categories to the list.
       For i = 0 To myCat2.Length - 1
          Me.listBox2.Items.Add(myCat2(i).CategoryName)
       Next
    End Sub
    
     private void btnGetCategories_Click(object sender, EventArgs e)
        {
            System.Diagnostics.PerformanceCounterCategory[] myCat2;
            // Clear the list's current contents.
            this.listBox2.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.listBox2.Items.Add(myCat2[i].CategoryName);
            }
        }
    

Vedere anche

Attività

Procedura: creare istanze del componente PerformanceCounter

Procedura: configurare istanze del componente PerformanceCounter

Concetti

Recupero dei valori del contatore delle prestazioni