一 PerformanceCounter 基本介绍
1 简单介绍
表示 Windows NT 性能计数器组件
命名空间:System.Diagnostics
程序集:System(在 system.dll 中)
2 构造函数(只介绍本文要用到的)
PerformanceCounter (String, String, String)
功能:
初始化 PerformanceCounter 类的新的只读实例,
并将其与本地计算机上指定的系统性能计数器或自定义性能计数器及类别实例关联
参数说明:
public PerformanceCounter (
string categoryName,
string counterName,
string instanceName
)
categoryName
性能计数器关联的性能计数器类别(性能对象)的名称。
counterName
性能计数器的名称。
instanceName
性能计数器类别实例的名称,或者为空字符串 (“”)(如果该类别包含单个实例)。
二 示例方法:
需要引用命名空间
using System.Diagnostics;
using System.Threading;
using System.Collections;
1 获取性能计数器类别列表
PerformanceCounterCategory 实例的 CategoryName 属性显示在“性能查看器”应用程序的“添加计数器”对话框的“性能对象”字段中。
PerformanceCounterCategory 类提供几种用于与计算机上的计数器和类别交互的方法。Create 方法使您能够定义自定义类别。Delete 方法提供从计算机移除类别的方法。GetCategories 方法使您能够查看类别的列表,而 ReadCategory 则检索与单个类别关联的所有计数器和实例数据。
性能计数器发布有关应用程序的性能数据。类别包括物理组件(如处理器、磁盘和内存)和系统对象(如进程和线程)。与同一性能对象相关的系统计数器归入一个指示其共同点的类别。当创建 PerformanceCounter 类的实例时,首先指示该组件将与之交互的类别,然后从该类别中选择一个计数器。
例如,一个 Windows 计数器类别属于“Memory”(内存)类别。此类别内的系统计数器跟踪内存数据,如可用字节数和缓存的字节数。如果要在应用程序中使用缓存的字节,则应创建 PerformanceCounter 组件的实例,将其连接到“Memory”(内存)类别,然后从该类别中选取相应的计数器(在这种情况下,选取“Cached Bytes”(缓存字节))。
虽然系统中有很多可用的计数器类别,但与之交互最频繁的可能是“Cache”(缓存)、“Memory”(内存)、“Objects”(对象)、“PhysicalDisk”(物理磁盘)、“Process”(进程)、“Processor”(处理器)、“Server”(服务器)、“System”(系统)和“Thread”(线程)等类别。
public static void GetCategoryNameList()
{
PerformanceCounterCategory[] myCat2;
myCat2 = PerformanceCounterCategory.GetCategories();
for (int i = 0; i < myCat2.Length; i++)
{
Console.WriteLine(myCat2[i].CategoryName.ToString());
}
}
注:GetCategories()使用时,会报错“参数“categoryName”的值“”无效。”此时 .NET不兼容问题,categoryName在4.0以下,4.5使用不了。
2 获取性能计数器类别下的实例的名称实例下的性能计数器的名称
public static void GetInstanceNameListANDCounterNameList(string CategoryName)
{
string[] instanceNames;
ArrayList counters = new ArrayList();
PerformanceCounterCategory mycat = new PerformanceCounterCategory(CategoryName);
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]));
}
}
for (int i = 0; i < instanceNames.Length; i++)
{
Console.WriteLine(instanceNames[i]);
}
Console.WriteLine("******************************");
foreach (PerformanceCounter counter in counters)
{
Console.WriteLine(counter.CounterName);
}
}
catch (Exception)
{
Console.WriteLine("Unable to list the counters for this category");
}
}
3 根据categoryName,counterName,instanceName获得性能情况显示`
private static void PerformanceCounterFun(string CategoryName, string InstanceName, string CounterName)
{
PerformanceCounter pc = new PerformanceCounter(CategoryName, CounterName, InstanceName);
while (true)
{
Thread.Sleep(1000); // wait for 1 second
float cpuLoad = pc.NextValue();
Console.WriteLine("CPU load = " + cpuLoad + " %.");
}
}
4 调用方法3显示cpu使用率
PerformanceCounterFun("Processor", "_Total", "% Processor Time");
三:PerformanceCounterCategory 方法
公共方法
名称 | 说明 |
---|---|
CounterExists | 已重载。 确定是否向特定的类别注册了指定的计数器。 |
Create | 已重载。 向系统注册自定义性能计数器类别和一个或多个计数器。 |
Delete | 从本地计算机移除类别及其关联的计数器。 |
Equals | 已重载。 确定两个 Object 实例是否相等。 (从 Object 继承) |
Exists | 已重载。 确定是否在系统上注册了该类别。 |
GetCategories | 已重载。 检索计算机上注册的性能计数器类别的列表。 |
GetCounters | 已重载。 检索此性能计数器类别中的计数器列表。 |
GetHashCode | 用作特定类型的哈希函数。 (从 Object 继承) |
GetInstanceNames | 已重载。 检索此性能计数器类别中的计数器列表。 |
GetType | 检索与此类别关联的性能对象实例列表。 |
InstanceExists | 已重载。 确定该类别是否包含指定的性能对象实例。 |
ReadCategory | 读取与此性能计数器类别关联的所有计数器和性能对象实例数据。 |
ReferenceEquals | 确定指定的 Object 实例是否是相同的实例。 (从 Object 继承) |
ToString | 返回表示当前 Object 的 String。 (从 Object 继承) |
受保护的方法
名称 | 说明 |
---|---|
Finalize | 允许 Object 在“垃圾回收”回收 Object 之前尝试释放资源并执行其他清理操作。 (从 Object 继承) |
MemberwiseClone | 创建当前 Object 的浅表副本。 (从 Object 继承) |
四:PerformanceCounterCategory 构造函数
重载列表
名称 | 说明 |
---|---|
PerformanceCounterCategory () | 初始化 PerformanceCounterCategory 类的新实例,让 CategoryName 属性保持为空,并将 MachineName 属性设置为本地计算机。 |
PerformanceCounterCategory (String) | 初始化 PerformanceCounterCategory 类的新实例,将 CategoryName 属性设置为指定的值,并将 MachineName 属性设置为本地计算机。 |
PerformanceCounterCategory (String, String) | 初始化 PerformanceCounterCategory 类的新实例,并将 CategoryName 和 MachineName 属性设置为指定的值。 |
五:PerformanceCounterCategory 属性
公共属性
名称 | 说明 |
---|---|
CategoryHelp | 获取类别的帮助文字。 |
CategoryName | 获取或设置定义此类别的性能对象的名称 |
CategoryType | 获取性能计数器类别类型。 |
MachineName | 获取或设置此类别所在的计算机的名称。 |