Working with performance counter

Blake81

Well-known member
Joined
Feb 23, 2006
Messages
304
Location
Georgia, USA
Programming Experience
1-3
How can I get a listbox to be filled with all of the possible options for PerformanCounter1.CategoryName and a second listbox to be filled with all of the options for CounterName, depending on which Category name I click in the first box? I can see how to set these values at design time, but I would like them in a listbox so that my program can monitor any performance data I choose at the time. Thanks.
 
shared method to use: PerformanceCounterCategory.GetCategories ()
 
Thanks, that works. How can I do the same thing for counter name and instance name? This is my code.

VB.NET:
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] cat [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] PerformanceCounterCategory
[/SIZE][SIZE=2][COLOR=#0000ff]For[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Each[/COLOR][/SIZE][SIZE=2] cat [/SIZE][SIZE=2][COLOR=#0000ff]In[/COLOR][/SIZE][SIZE=2] PerformanceCounterCategory.GetCategories
ListBox3.Items.Add(cat.CategoryName)
[/SIZE][SIZE=2][COLOR=#0000ff]Next[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]
[/COLOR][/SIZE]
 
I got it.

VB.NET:
[SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] ListBox3_SelectedIndexChanged([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Object, [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.EventArgs) [/SIZE][SIZE=2][COLOR=#0000ff]Handles[/COLOR][/SIZE][SIZE=2] ListBox3.SelectedIndexChanged
ListBox4.Items.Clear()
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] cat2 [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] PerformanceCounterCategory
cat2.CategoryName = ListBox3.SelectedItem
cat2.CategoryName = ListBox3.SelectedItem
ListBox4.Items.AddRange(cat2.GetInstanceNames)
[/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]
[/COLOR][/SIZE]
 
Here's code example using 3 listboxes, ListBox1-2-3. Call method perfCounterCategories to display categories in listbox1 (sorted by name). If you select a category, its instances are listed in listbox2. If there is only one instance it will not list (GetInstanceNames return a single item array with empty string), but here the counters of that instance are automatically displayed in listbox3. If there are more than one instance the names are displayed in listbox2, select one to have the counters display in listbox3.
VB.NET:
Sub perfCounterCategories()
    [COLOR=darkgreen]'display categories in listbox1[/COLOR]
    Dim pccs() As PerformanceCounterCategory = PerformanceCounterCategory.GetCategories()
    Dim pccnames(pccs.Length - 1) As String
    For i As Integer = 0 To pccs.Length - 1
        pccnames(i) = pccs(i).CategoryName
    Next
    Array.Sort(pccnames)
    ListBox1.DataSource = pccnames
End Sub
 
Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles ListBox1.SelectedIndexChanged
    [COLOR=darkgreen]'display instances in listbox2[/COLOR]
    Dim pcc As New PerformanceCounterCategory(ListBox1.SelectedValue)
    ListBox2.DataSource = pcc.GetInstanceNames
    If ListBox2.Items.Count = 0 Then
        [COLOR=darkgreen]'display counters in listbox3[/COLOR]
        ListBox3.DataSource = pcc.GetCounters
        ListBox3.DisplayMember = "CounterName"
    End If
End Sub
 
Private Sub ListBox2_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles ListBox2.SelectedIndexChanged
    [COLOR=darkgreen]'display counters in listbox3[/COLOR]
    Dim pcc As New PerformanceCounterCategory(ListBox1.SelectedValue)
    ListBox3.DataSource = pcc.GetCounters(ListBox2.SelectedValue)
    ListBox3.DisplayMember = "CounterName"
End Sub
 
Back
Top