C#: PerformanceCounter的使用
在实际编程中,有的时候需要密切注意CPU, Memory的变化。这个时候需要用到PerformanceCounter这个类,注意需要using System.Diagnostics;
这里只是在console上进行了一些测试,每一个CategoryName都有很多个CounterName。不需要对所有的CounterName都了解
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Threading;
namespace ConsoleTest
{
class Program
{
public static void GetCategoryNameList()
{
PerformanceCounterCategory[] myCat2 = PerformanceCounterCategory.GetCategories();
; i < myCat2.Length; i++)
{
Console.WriteLine(myCat2[i].CategoryName);
}
}
public static void GetInstanceNameListANDCounterNameList(string CategoryName)
{
string[] instanceNames;
List<PerformanceCounter> counters = new List<PerformanceCounter>();
PerformanceCounterCategory mycat = new PerformanceCounterCategory(CategoryName);
try
{
instanceNames = mycat.GetInstanceNames();
)
{
counters.AddRange(mycat.GetCounters());
}
else
{
; i < instanceNames.Length; i++)
{
counters.AddRange(mycat.GetCounters(instanceNames[i]));
}
}
; 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");
}
}
private static void PerformanceCounterFun(string CategoryName, string CounterName, string InstanceName)
{
PerformanceCounter pc = new PerformanceCounter(CategoryName, CounterName, InstanceName);
while (true)
{
Thread.Sleep(); // wait for 1 second
float Load = pc.NextValue();
Console.WriteLine(CounterName + ": " + Load);
}
}
static void Main(string[] args)
{
//GetCategoryNameList();
GetInstanceNameListANDCounterNameList("Memory");
//PerformanceCounterFun("Processor", "% Processor Time", "_Total");
//PerformanceCounterFun("Processor", "Working Set", "_Total");
//PerformanceCounterFun("Memory", "% Committed Bytes In Use", "");
PerformanceCounterFun("Memory", "Available MBytes", "");
}
}
}
C#: PerformanceCounter的使用的更多相关文章
- 利用Windows性能计数器(PerformanceCounter)监控
一.概述 性能监视,是Windows NT提供的一种系统功能.Windows NT一直以来总是集成了性能监视工具,它提供有关操作系统当前运行状况的信息,针对各种对象提供了数百个性能计数器.性能对象,就 ...
- PerformanceCounter蛋痛的设计
在.NET下对进程的性能计数可以使用PerformanceCounter,通过该对象可以对进程的CPU,内存等信息进行统计.对于正常使用来说这个对象还是很方便,但对于同一名称的多个进程进行性能计数那真 ...
- C# 使用 PerformanceCounter 获取 CPU 和 硬盘的使用率
C# 使用 PerformanceCounter 获取 CPU 和 硬盘的使用率: 先看界面: 建一个 Windows Form 桌面程序,代码如下: using System; using Sys ...
- 计算机系统监控 PerformanceCounter
PerformanceCounter 컴퓨터 성능 머니터링 CUP Processor 메모리 하터웨어 DB (CPU,User Connection,Batch Request,Blocking ...
- C#透过PerformanceCounter取得特定Process的CPU使用率
- C# 利用性能计数器监控网络状态
本例是利用C#中的性能计数器(PerformanceCounter)监控网络的状态.并能够直观的展现出来 涉及到的知识点: PerformanceCounter,表示 Windows NT 性能计数器 ...
- 使用PowerShell收集多台服务器的性能计数器
写在前面 当管理多台Windows Server服务器时(无论是DB.AD.WEB以及其他的应用服务器),当出现性能或其他问题后,参阅性能计数器都是一个非常好的维度从而推测出问题可能出现的原因 ...
- C#获取CPU占用率、内存占用、磁盘占用、进程信息
代码: using System; using System.Collections.Generic; using System.Diagnostics; using System.Threading ...
- asp.net mvc4 简单的服务器监控开发之C#获取服务器CPU、RAM、TCP等系统信息(上)
一.背景 前段时间服务器出了点问题,加上学业愈来愈紧张,写博文分享的时间越来越少.虽然不是第一次在博客园上写经验,但是近期分享的博文得到了不少的朋友支持和指正,在这里内心非常感激和开心.希望以后能认真 ...
随机推荐
- 【转】android中最好的瀑布流控件PinterestLikeAdapterView
[源地址]http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2014/0919/1696.html 之前我们介绍过一个开源的瀑布流控件Stag ...
- 【运维工具】logrotate 日志管理神器
服务器经常会产生各种各样的日志文件,我们需要定期清理 日志的分类 系统日志 应用日志 系统日志 例如系统的history 历史信息 crontab的运行日志 一般系统日志系统都帮我们运维好了,不 ...
- nRF51822之模拟IIC
使用的工程为是基于sdk10工程 在将以nRF51_SDK_10.0.0_dc26b5e\examples\peripheral\twi_sensor作为模版 修改代码main.c #include ...
- Makefile 如何轻松搞定
最近在学习Linux下的C编程,买了一本叫<Linux环境下的C编程指南>读到makefile就越看越迷糊,可能是我的理解能不行. 于是google到了以下这篇文章.通俗易懂.然后把它贴出 ...
- nginx常用变量
$args, 请求中的参数; $content_length, HTTP请求信息里的”Content-Length”; $content_type, 请求信息里的”Content-Type”; $do ...
- HBASE的读写以及client API
一:读写思想 1.系统表 hbase:namespace 存储hbase中所有的namespace的信息 hbase:meta rowkey:hbase中所有表的region的名称 column:re ...
- 【Android开发学习笔记】【第五课】Activity的生命周期-上
今天学习Activity当中的七个生命周期函数: 首先得说一个事情,就是在代码当中如果加入了 System.out.println(" ------");之后,如何查看这里面的输出 ...
- jdk 8 lambda表达式 及在Android Studio的使用示例
以前觉得java让人觉得有趣的一个特点是支持:匿名内部类,而最近发现jdk8已支持lambda并有更简便的方式,来实现匿名内部类. 这会让程序员更舒服,更喜欢java. 多年前觉得java语法和C#语 ...
- 如何方便的控制css3动画开始时间点与持续时间
一般我们在控制css3 animate动画时可以通过简写以减少代码量,只要在需要动画的元素上追加一下类名就可以了,如下例子 /*淡入并向上移动一点位置出现*/ .fadeInUp{ -webkit-a ...
- Asp.net MVC23 使用Areas功能的常见错误
一般WEB项目都会不同的页面区域,如:用户前台.用户后台.管理员后台. 访问的URL: 用户前台:www.domain.com/home/index 用户后台:www.domain.com/admin ...