PerformanceCounter 基本介绍以及示例方法
2010-02-25 11:44
281 查看
一 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
性能计数器类别实例的名称,或者为空字符串 ("")(如果该类别包含单个实例)。
二 示例方法:
需要引用命名空间
1 获取性能计数器类别列表
虽然系统中有很多可用的计数器类别,但与之交互最频繁的可能是“Cache”(缓存)、“Memory”(内存)、
“Objects”(对象)、“PhysicalDisk”(物理磁盘)、“Process”(进程)、“Processor”(处理器)、
“Server”(服务器)、“System”(系统)和“Thread”(线程)等类别
2 获取性能计数器类别下的实例的名称实例下的性能计数器的名称
3 根据categoryName,counterName,instanceName获得性能情况显示
4 调用方法3显示cpu使用率
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 获取性能计数器类别列表
虽然系统中有很多可用的计数器类别,但与之交互最频繁的可能是“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()); } }
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", "% Processor Time","_Total");
相关文章推荐
- [转]PerformanceCounter 基本介绍以及示例方法
- PerformanceCounter 基本介绍以及示例方法
- PerformanceCounter 基本介绍以及示例方法
- PerformanceCounter 基本介绍以及示例方法
- PerformanceCounter 基本介绍以及示例方法
- PerformanceCounter 基本介绍以及示例方法
- PerformanceCounter 基本介绍以及示例方法(转载)
- PerformanceCounter 基本介绍以及示例方法
- PerformanceCounter 基本介绍以及示例方法
- 对javascript基本对象的属性以及方法的实例介绍
- 排序算法之冒泡排序的基本介绍和数组越界,以及交换2个变量的3种方法
- java 集合 之 链表和线性表以及ArrayList的各方法的介绍及示例 及 ArrayList与LinkedList的区别分析 及 ArrayList与Array(数组)的区别
- android中SQLite数据库的基本操作以及重要方法介绍
- CentOS7日期时间设置方法以及时间基本概念介绍
- 对javascript基本对象的属性以及方法的实例介绍
- day10 反射创建数组 线程的基本概念 线程的编写和启动方式 线程的运行状态以及状态转换方法 线程的调度和优先级设置
- TClientDataSet的基本属性和方法介绍
- GPU(CUDA)学习日记(三)------ CUDA基本架构介绍以及编程入门!~~
- mysql清除日志以及mysql清空表的方法介绍
- 关于Arduino 使用软pwm的方法以及与上位机的通讯(带示例)