在实际编程中,有的时候需要密切注意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. SQLServer2008:助您轻松编写T-SQL存储过程(原创)【转】

      本文主要介绍 SQLServerExpress2008不用第三方工具调试T-SQL语句,经过本文的介绍,用SQLSERVER2008 Manage studio 编写.调试T-SQL存储过程都将是 ...

  2. NSURLSession

    参考文章1, apple文档 一.NSURLSessionConfiguration 介绍:分别配置每一个 session 对象.(NSURLConnection 很难做到) 分类: 1) defau ...

  3. php+mysql实现事务回滚

    模拟条件:第一个表插入成功,但是第二个表插入失败,回滚.第一个表插入成功,第二个表插入成功,执行.第一个表插入失败,第二个表插入成功,回滚.第一个表插入失败,第二个表插入失败,回滚.以上情况都需要回滚 ...

  4. Apache的HBase与cdh的hue集成(不建议不同版本之间的集成)

    1.修改hue的配置文件hue.ini [hbase] # Use full hostname with security. hbase_clusters=(Cluster|linux-hadoop3 ...

  5. 浅析在QtWidget中自定义Model

    Qt 4推出了一组新的item view类,它们使用model/view结构来管理数据与表示层的关系.这种结构带来的功能上的分离给了开发人员更大的弹性来定制数据项的表示,它也提供一个标准的model接 ...

  6. Linux GDB调试全面解析

    GDB是GNU开源组织发布的一个强大的UNIX下的程序调试工具,GDB主要可帮助工程师完成下面4个方面的功能: 启动程序,可以按照工程师自定义的要求随心所欲的运行程序. 让被调试的程序在工程师指定的断 ...

  7. [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 ...

  8. 使用git Rebase让历史变得清晰

    当多人协作开发一个分支时,历史记录通常如下方左图所示,比较凌乱.如果希望能像右图那样呈线性提交,就需要学习git rebase的用法. “Merge branch”提交的产生 我们的工作流程是:修改代 ...

  9. Segments---poj3304(判断直线与线段的位置关系)

    题目链接:http://poj.org/problem?id=3304 题意:给你n个线段,求是否有一条直线与所有的线段都相交,有Yes,没有No; 枚举所有的顶点作为直线的两点,然后判断这条直线是否 ...

  10. TortoiseGit 的使用

    日常用法 (1) 创建新库 在文件夹中按右键, 选择Git Create repository here 就可以创建库了. 在出现的窗口中, 不勾选选项, 直接按OK 在目录中就会出现一个名为.git ...