演练:检索类别和计数器
更新:2007 年 11 月
此演练中的过程带您学习以下过程:创建和配置 PerformanceCounter 组件实例以及使用它们检索系统上性能计数器类别和计数器的列表。性能计数器是 Windows 收集不同系统资源上性能数据的方法。Windows 包含一组预定义的按类别组织的计数器,您可以与其交互。每个类别和计数器都与系统功能的特定区域相关。
在此演练中,将:
实例化 PerformanceCounter 组件,并将它配置为与系统生成的特定类别的计数器交互。
创建在列表框中显示有关类别和计数器的信息的 Windows 应用程序。
使用 GetCategories 方法返回本地计算机上类别的列表。
使用 GetCounters 方法从指定的类别返回计数器的列表。
创建 Windows 应用程序
在“新建项目”对话框中,创建 Visual Basic、Visual C# 或 Visual J#“Windows 应用程序”。
从“工具箱”的“Windows 窗体”选项卡,向窗体中添加两个按钮和两个列表框。以喜欢的任何顺序排列它们,然后为它们设置下列属性:
控件
属性
值
Button1
Name
btnGetCounters
Text
获取计数器
Button2
Name
btnGetCategories
Text
获取类别
ListBox1
Name
lstCounters
ScrollAlwaysVisible
True
ListBox2
Name
lstCategories
ScrollAlwaysVisible
True
保存您的工作。
检索类别列表
在“设计”视图中,双击“获取类别”按钮以访问代码编辑器。
在 Visual J# 版本中,在屏幕的顶部添加引用 System.Diagnostics 命名空间的 import 语句。
在 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); } }
检索计数器列表
在“设计”视图中,双击“获取计数器”按钮以访问代码编辑器。插入点将置于 btnGetCounters_Click 事件中。
添加下列代码,从所选类别检索计数器的列表。
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); } } }
测试应用程序
保存并编译应用程序。
单击“获取类别”按钮。您将会在列表框中看到一个类别列表。
选择其中的类别之一,然后单击“获取计数器”按钮。您将会看到所选类别的计数器的列表。