一 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 获取性能计数器类别列表
虽然系统中有很多可用的计数器类别,但与之交互最频繁的可能是“Cache”(缓存)、“Memory”(内存)、
“Objects”(对象)、“PhysicalDisk”(物理磁盘)、“Process”(进程)、“Processor”(处理器)、
“Server”(服务器)、“System”(系统)和“Thread”(线程)等类别

[c-sharp] view plain copyprint?
  1. public static void GetCategoryNameList()
  2. {
  3. PerformanceCounterCategory[] myCat2;
  4. myCat2 = PerformanceCounterCategory.GetCategories();
  5. for (int i = 0; i < myCat2.Length; i++)
  6. {
  7. Console.WriteLine(myCat2[i].CategoryName);
  8. }
  9. }

public static void GetCategoryNameList() { PerformanceCounterCategory[] myCat2; myCat2 = PerformanceCounterCategory.GetCategories(); for (int i = 0; i < myCat2.Length; i++) { Console.WriteLine(myCat2[i].CategoryName); } }

2 获取性能计数器类别下的实例的名称实例下的性能计数器的名称

[c-sharp] view plain copyprint?
  1. public static void GetInstanceNameListANDCounterNameList(string CategoryName)
  2. {
  3. string[] instanceNames;
  4. ArrayList counters = new ArrayList();
  5. PerformanceCounterCategory mycat = new PerformanceCounterCategory(CategoryName);
  6. try
  7. {
  8. instanceNames = mycat.GetInstanceNames();
  9. if (instanceNames.Length == 0)
  10. {
  11. counters.AddRange(mycat.GetCounters());
  12. }
  13. else
  14. {
  15. for (int i = 0; i < instanceNames.Length; i++)
  16. {
  17. counters.AddRange(mycat.GetCounters(instanceNames[i]));
  18. }
  19. }
  20. for (int i = 0; i < instanceNames.Length; i++)
  21. {
  22. Console.WriteLine(instanceNames[i]);
  23. }
  24. Console.WriteLine(”******************************”);
  25. foreach (PerformanceCounter counter in counters)
  26. {
  27. Console.WriteLine(counter.CounterName);
  28. }
  29. }
  30. catch (Exception)
  31. {
  32. Console.WriteLine(”Unable to list the counters for this category”);
  33. }
  34. }

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获得性能情况显示

[c-sharp] view plain copyprint?
  1. private static void PerformanceCounterFun(string CategoryName, string InstanceName, string CounterName)
  2. {
  3. PerformanceCounter pc = new PerformanceCounter(CategoryName, CounterName, InstanceName);
  4. while (true)
  5. {
  6. Thread.Sleep(1000); // wait for 1 second
  7. float cpuLoad = pc.NextValue();
  8. Console.WriteLine(”CPU load = ” + cpuLoad + “ %.”);
  9. }
  10. }

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”);

原文地址:http://www.cnblogs.com/xh831213/archive/2008/06/12/1218234.html

2.PerformanceCounter简述及用法相关推荐

  1. PerformanceCounter简述及用法

    一 PerformanceCounter简述 1 简单介绍 表示 Windows NT 性能计数器组件 命名空间:System.Diagnostics 程序集:System(在 system.dll ...

  2. Unity协程简述(简单用法,简易分析)

    Unity协程 协程的简单用法 简述 函数 协程的执行顺序 协程替我们做了什么 Yleid Return 如何进行跳帧,延迟,等待的操作 从IL的角度分析 IL语言 总结 自定义一个迭代器 优化 最后 ...

  3. Promise简述及用法

    什么是Promise Promise 是异步编程的一种解决方案,比传统的解决方案--回调函数和事件--更合理且更强大.它最早由社区提出并实现,ES6将其写进了语言标准,统一了用法,并原生提供了Prom ...

  4. (操作系统题目题型总结)第三章:同步与互斥

    费翔林课本习题 思考题 1.试述顺序程序设计的特点以及采用顺序程序设计的优缺点 [答案] 特点: 执行的顺序性:一个程序在处理器上是严格按序执行的,每个操作必须在下一个操作开始前结束 环境的封闭性:运 ...

  5. Android阿面试积累,android项目开发实战密码

    公差为1的等差数列求和,如何优化 自己的优势在哪里 注解如何获取,反射为何耗性能? Java的GC机制,分代回收策略 Binder机制:ServiceManager什么时候注册的? int,long的 ...

  6. Android软件开发面试题,安卓面试题库

    一.开始的开始 **Android框架体系架构(高级UI+FrameWork源码)**这块知识是现今使用者最多的,我们称之Android2013~2016年的技术,但是,即使是这样的技术,Androi ...

  7. 2020面试题合集之吊打面试官系列(一),Android中为什么需要Handler

    合并式:addAssetPath时加入所有插件和主工程的路径:由于AssetManager中加入了所有插件和主工程的路径,因此生成的Resource可以同时访问插件和主工程的资源.但是由于主工程和各个 ...

  8. 2019最新中级Android面试题目,有着几篇就够了,Android开发五年

    全埋点:全埋点指的是将Web页面/App内产生的所有的.满足某个条件的行为,全部上报到后台服务器 可视化埋点:通过可视化工具(例如Mixpanel)配置采集节点,在Android端自动解析配置并上报埋 ...

  9. 吊打面试官:Android中高级面试题 -- 终局之战,万分膜拜

    提供服务的业务模块: 在公共服务(CommonService) 中声明 Service 接口 (含有需要被调用的自定义方法), 然后在自己的模块中实现这个 Service 接口, 再通过 ARoute ...

最新文章

  1. HttpServletResponse对象(一)
  2. 样式集(11)注册页面样式,全部代码附效果图
  3. 人人都是 API 设计者:我对 RESTful API、GraphQL、RPC API 的思考
  4. 产品认识:一个可直接套用的产品分析框架(纯干货)
  5. 利用kali的msf提取汇编机器码(shellcode)
  6. Eclipse JAVA项目的 目录结构 和 导入 import菜单使用
  7. 导致大量kworker的原因_氨氮超标的几种原因及解决办法
  8. (216)滤波器介绍
  9. 矜情作态的拼音及解释
  10. 动视服务器状态,《使命召唤12》A.B.C服务器错误不用怕 动视给你支招
  11. Eclipse中Build Workspace 优化
  12. (转)fiddler使用简介--其二
  13. Google专卖店顾客消费预测问题:如何将数据的json格式转换成csv格式
  14. Axure中推动拉动元件不生效_mac系统axure元件不能拖动的非正常解决方法
  15. matlab 滑动平均窗滤波,滑动平均滤波器与CIC滤波器
  16. MUI框架-08-窗口管理-创建子页面
  17. three.js 实现露珠滴落动画
  18. 建一个网站需要多少钱
  19. C语言---简单五子棋小游戏
  20. windows开机启动自定义程序和任务

热门文章

  1. MATLAB神经网络——BP神经网络训练过程介绍(newff)
  2. ET为什么使用非阻塞IO
  3. win安装doccano_支持多语言的文本标注工具——doccano
  4. 深入MyBatis开发之mybatis映射器
  5. ofo遭遇线上退押金挤兑潮:排队退款人数已超上千万人
  6. Linux环境下服务启动命令汇总
  7. 大型供应链物流企业的数字化转型方法论
  8. 【每日早报】201/10/28
  9. Tableau填充地图、多维地图、混合地图
  10. Stanford Corenlp