在实际编程中,有的时候需要密切注意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. composer 272解决

    composer global require "fxp/composer-asset-plugin:~1.0.3"                                 ...

  2. nrf51822裸机教程-SPI(主)

    关于SPI总线的介绍这里就不细说了,网上有很多介绍SPI总线时序的. SPI总线的本质就是一个环形总线结构,在时钟驱动下两个双向移位寄存器进行数据交换. 所以SPI总线的特色就是:传输一字节数据的同时 ...

  3. nodejs express测试

    1.页面请求 app.get('/list_user', function (req, res) { console.log("/list_user GET 请求"); //res ...

  4. Servlet Threading Model

    Servlet Threading Model The scalability issues of Java servlets are caused mainly by the server thre ...

  5. [LeetCode] Substring with Concatenation of All Words(good)

    You are given a string, S, and a list of words, L, that are all of the same length. Find all startin ...

  6. 多选列表Select之双击删除与添加Demo

    双击任一Select控件,查看效果: srcA srcC srcB targetC targetB targetA   源码: <html> <head> <script ...

  7. SQL Server 2008 R2中,“选择前1000行”为“选择所有行”

    从SQL Server 2008开始,微软为了提高查询效率等原因,右键点击表时弹出菜单中默认没有"显示所有行",而以"选择前1000行"替代. 点击[工具]-& ...

  8. busybox sz rz命令

    之前板子和电脑之间传送文件的时候都是通过tftp网络下载.所以找了一下在文件系统中使用串口上传文件的方法. rz和sz命令使用zmoderm协议,SecureCRT也用提供这个命令的支持.由于是串口, ...

  9. imx6 android 进入文件系统闪屏

    imx6进入文件系统的时候都会闪屏,应该是framebuffer未初始化,就已经打开了背光.目前解决办法,在kenel阶段关闭背光,显示android的开机动画之后(此时framebuffer已经初始 ...

  10. 怎么使用git来管理项目版本?

    怎么使用git来管理项目版本和存放代码? 作者:rongfangliu 转载请注明出处:http://www.cnblogs.com/rongfangliu/p/howuseGit.html 工具: ...