在实际编程中,有的时候需要密切注意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的使用的更多相关文章

  1. 利用Windows性能计数器(PerformanceCounter)监控

    一.概述 性能监视,是Windows NT提供的一种系统功能.Windows NT一直以来总是集成了性能监视工具,它提供有关操作系统当前运行状况的信息,针对各种对象提供了数百个性能计数器.性能对象,就 ...

  2. PerformanceCounter蛋痛的设计

    在.NET下对进程的性能计数可以使用PerformanceCounter,通过该对象可以对进程的CPU,内存等信息进行统计.对于正常使用来说这个对象还是很方便,但对于同一名称的多个进程进行性能计数那真 ...

  3. C# 使用 PerformanceCounter 获取 CPU 和 硬盘的使用率

    C# 使用 PerformanceCounter 获取 CPU 和 硬盘的使用率: 先看界面: 建一个 Windows Form  桌面程序,代码如下: using System; using Sys ...

  4. 计算机系统监控 PerformanceCounter

    PerformanceCounter 컴퓨터 성능 머니터링 CUP Processor 메모리 하터웨어 DB (CPU,User Connection,Batch Request,Blocking ...

  5. C#透过PerformanceCounter取得特定Process的CPU使用率

  6. C# 利用性能计数器监控网络状态

    本例是利用C#中的性能计数器(PerformanceCounter)监控网络的状态.并能够直观的展现出来 涉及到的知识点: PerformanceCounter,表示 Windows NT 性能计数器 ...

  7. 使用PowerShell收集多台服务器的性能计数器

    写在前面     当管理多台Windows Server服务器时(无论是DB.AD.WEB以及其他的应用服务器),当出现性能或其他问题后,参阅性能计数器都是一个非常好的维度从而推测出问题可能出现的原因 ...

  8. C#获取CPU占用率、内存占用、磁盘占用、进程信息

    代码: using System; using System.Collections.Generic; using System.Diagnostics; using System.Threading ...

  9. asp.net mvc4 简单的服务器监控开发之C#获取服务器CPU、RAM、TCP等系统信息(上)

    一.背景 前段时间服务器出了点问题,加上学业愈来愈紧张,写博文分享的时间越来越少.虽然不是第一次在博客园上写经验,但是近期分享的博文得到了不少的朋友支持和指正,在这里内心非常感激和开心.希望以后能认真 ...

随机推荐

  1. 小米2/2S 手机由 Smartisan OS ROM 刷回 MIUI 教程

    刷机所需文件1.请前往 MIUI 官网下载小米 2/2S 标准版 MIUI ROM 包.下载地址:http://www.miui.com/getrom.php?r=2:下载后请将原文件名修改为“upd ...

  2. scala手动编译运行

    1  Person.scala class Person { var name = "" } object Person { // a one-arg constructor de ...

  3. Bluetooth Baseband介绍

    目录 1. 概述 1.1 Clock(时钟) 1.2 寻址方式 2. 物理信道(Physical Channels) 3. 物理链路(Physical Links) 4. 逻辑传输层(Logical ...

  4. 蓝牙的L2CAP协议

    1.概述     L2CAP能向上层提供面向连接的或者无连接的数据服务,拥有multiplexing capability and segmentation and reassembly operat ...

  5. java JDK8 学习笔记——第11章 线程和并行API

    第11章 线程与并行API 11.1 线程 11.1.1 线程 在java中,如果想在main()以外独立设计流程,可以撰写类操作java.lang.Runnable接口,流程的进入点是操作在run( ...

  6. linux下时间的修改

    1.关于时间的修改,在linux还是很重要的,在这里只是介绍一个简单的常用的命令,并且时间不会写入到系统. 2.命令 3.如果想把时间写进系统 修改完成之后,输入clock -w 时间将会被写进CMO ...

  7. 过滤android应用列表(区分系统应用、第三方应用、sd卡中的应用)

    if ((app.flags & ApplicationInfo.FLAG_SYSTEM) != 0) { // 系统程序 }else if ((app.flags & Applica ...

  8. alibaba的FastJson(高性能JSON开发包)

    这是关于FastJson的一个使用Demo,在Java环境下验证的 class User{ private int id; private String name; public int getId( ...

  9. Shell Script-读取配置文件

    需求 有时候在shell script里面需要一些执行,如果放在程序里面不便于统一管理,并且每一次修改路径都要去script里面改好麻烦,所以统一把路径放在配置文件中方便管理. 问题 如何读取相对应的 ...

  10. 【python PIL学习】给照片打水印

    不知道别人是怎么做的,闲着无聊就根据前几天看到的一些PIL里面的函数,写了这个小程序,经过测试,基本可以给所有像宽400以上的图片打上从左下到右上的看上去质量尚可的水印,更小的图片水印偏下.写完觉得很 ...