得到系统的进程使用情况,性能计数器的使用

using  System;
using  System.Diagnostics;
using  System.Threading;


public class CpuLoadInfo
{

    //性能计数器
    private static PerformanceCounter pc = new PerformanceCounter();
    /// <summary>
    /// 得到CPU占用时间的百分比
    /// </summary>
    /// <returns>返回CPU占用时间的百分比</returns>
    public static float getCPUInUsedRate()
    {
        pc.CategoryName = "Processor";
        pc.CounterName = "% Processor Time";
        pc.InstanceName = "_Total";
        pc.ReadOnly = true;
        return pc.NextValue();
    }
    public static float getMemoryAuailable()
    {
        pc.CategoryName = "Memory";
        pc.CounterName = "Available MBytes";
        pc.ReadOnly = true;
        return pc.NextValue();
    }
    public static float getMemoryCommitted()
    {
        pc.CategoryName = "Memory";
        pc.CounterName = "CommittedBytes";
        pc.ReadOnly = true;
        return pc.NextValue() / (1024 * 1024);
    }
    public static float getMSMQMessagesMBytes(string sInstanceName)
    {
        pc.CategoryName = "MSMQ Queue";
        pc.CounterName = "Bytes in Queue";
        pc.InstanceName = sInstanceName;
        pc.ReadOnly = true;
        try
        {
            return pc.NextValue() / (1024 * 1024);
        }
        catch (Exception ex)
        {
            return 0;
        }
    }
    public static float getMSMQSerives()
    {
        return 0;
    }
   
    [STAThread]
    public static void Main()
    {
 

        //PerformanceCounterCategory.Create(("Processor", "%  Processor  Time", "_Total");
        PerformanceCounter pc1
          = new PerformanceCounter();
       // pc.MachineName ="172.10.100.201";
        pc1.CategoryName = "MSMQ Queue";
        pc1.CounterName = "Bytes in Queue";

        pc1.InstanceName = Environment.MachineName+"//private$//coreexchange";
        pc1.ReadOnly = true;
        PerformanceCounter pc2
      = new PerformanceCounter();
        // pc.MachineName ="172.10.100.201";
        pc2.CategoryName = "Memory";
        pc2.CounterName = "System Driver Total Bytes";
       // pc2.InstanceName = "_Total";
        pc2.ReadOnly = true;


        while (true)
        {
            float cpuLoad1,cpuLoad2;
            Thread.Sleep(1000);  //  wait  for  1  second
            cpuLoad1 = pc1.NextValue();
            cpuLoad2 = pc2.NextValue();
            //Console.WriteLine("CPU  load  =  " + cpuLoad1 / (1024 * 1024));
           Console.WriteLine(cpuLoad2 / (1024 * 1024));
          // Console.WriteLine(getCPUInUsedRate());
        }
    }
   
    private const string CategoryName = "Process";
    private const string CounterName = "Private Bytes";
    private const string InstanceName = "mqsvc";

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值