Procedura dettagliata: recupero di categorie e contatori
Aggiornamento: novembre 2007
In questo argomento vengono illustrati i processi di creazione e di configurazione delle istanze del componente PerformanceCounter e il relativo utilizzo per il recupero di elenchi di categorie di contatori delle prestazioni e contatori nel sistema. Il contatore delle prestazioni rappresenta il meccanismo utilizzato in Windows per raccogliere i dati sulle prestazioni delle varie risorse di sistema. In Windows è disponibile un gruppo di contatori predefiniti, organizzati in categorie, con cui è possibile interagire. Ogni categoria e ogni contatore è correlato a un determinato insieme di funzionalità di sistema.
In questa procedura dettagliata vengono illustrate le seguenti operazioni:
Creazione di un'istanza del componente PerformanceCounter e relativa configurazione per interagire con una specifica categoria di contatori generati dal sistema.
Creazione di un'applicazione Windows in cui vengono visualizzate informazioni sulle categorie e sui contatori all'interno di una casella di riepilogo.
Utilizzo del metodo GetCategories per restituire un elenco di categorie sul computer locale.
Utilizzo del metodo GetCounters per restituire un elenco di contatori dalla categoria specificata.
Per creare un'applicazione Windows
Dalla finestra di dialogo Nuovo progetto creare un'Applicazione Windows in Visual Basic, Visual C# o Visual J#
Dalla scheda Windows Form della Casella degli strumenti aggiungere al form due pulsanti e due caselle di riepilogo. Definire l'ordine desiderato, quindi impostare le seguenti proprietà:
Controllo
Proprietà
Valore
Button1
Name
btnGetCounters
Text
Get Counters
Button2
Name
btnGetCategories
Text
Get Categories
ListBox1
Name
lstCounters
ScrollAlwaysVisible
True
ListBox2
Name
lstCategories
ScrollAlwaysVisible
True
Salvare il lavoro svolto.
Per recuperare l'elenco delle categorie
In visualizzazione Progettazione fare doppio clic sul pulsante Get Categories per accedere all'editor del codice.
Nella versione Visual J#, nella parte superiore dello schermo aggiungere un'istruzione import con riferimento allo spazio dei nomi System.Diagnostics.
Nella routine btnGetCategories_Click aggiungere il codice riportato di seguito per recuperare l'elenco delle categorie del computer locale.
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); } }
Per recuperare l'elenco dei contatori
In visualizzazione Progettazione fare doppio clic sul pulsante Get Counters per accedere all'editor del codice. Il punto di inserimento verrà posizionato sull'evento btnGetCounters_Click.
Aggiungere il codice riportato di seguito per recuperare l'elenco dei contatori dalla categoria selezionata.
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); } } }
Per eseguire il test dell'applicazione
Salvare e compilare l'applicazione.
Fare clic sul pulsante Get Categories. Nella casella di riepilogo verrà visualizzato un elenco di categorie.
Selezionare una delle categorie e fare clic sul pulsante Get Counters. Verrà visualizzato un elenco di contatori per le categorie selezionate.
Vedere anche
Concetti
Introduzione al monitoraggio dei valori limite delle prestazioni