VS2005下PerformanceCounter的使用
如题,最近在研究这个,遇到几个问题,希望大家能帮帮我!程序代码:
using System; using System.Collections.Generic; using using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Diagnostics; namespace Sample { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private int[] count = new int[13]; private int flag = 1; private void RefData() //每一秒更新一次 { PerformanceCounter pc; pc = new PerformanceCounter("Process", "Handle Count", "_Total"); //句柄 count[0] = Convert.ToInt32(pc.NextValue()); pc = new PerformanceCounter("Process", "Thread Count", "_Total"); //线程 count[1] = Convert.ToInt32(pc.NextValue()); Process[] p = Process.GetProcesses(); //进程 count[2] = p.Length; pc = new PerformanceCounter("Memory", "Available KBytes"); //可用物理内存 count[4] = Convert.ToInt32(pc.NextValue()); pc = new PerformanceCounter("Memory", "Cache Bytes"); //获取系统缓存,这里不对,谁知道这里应该怎么写? count[5] = Convert.ToInt32(pc.NextValue() / 1024); pc = new PerformanceCounter("Memory", "Committed Bytes"); //使用中的物理内存 count[6] = Convert.ToInt32(pc.NextValue() / 1024); pc = new PerformanceCounter("Memory", "Pool Paged Bytes"); //核心内存分页 count[10] = Convert.ToInt32(pc.NextValue() / 1024); pc = new PerformanceCounter("Memory", "Pool Nonpaged Bytes"); //核心内存未分页 count[11] = Convert.ToInt32(pc.NextValue() / 1024); count[9] = count[10] + count[11]; //核心内存总数,为分页和未分页核心内存的和 if (flag == 1) { count[3] = count[4] + count[6]; //一次性统计物理内存总数,这里是使用的物理内存和没有使用中的物理内存之和,有点偏差,请问如何改?就是如何获取物理内存总量! pc = new PerformanceCounter("Memory", "Commit Limit"); //一次性统计最大物理内存使用量 count[7] = Convert.ToInt32(pc.NextValue() / 1024); count[8] = count[6]; flag = 0; } else { pc = new PerformanceCounter("Memory", "Committed Bytes"); //获取内存使用最大值,与前次比较 int i = Convert.ToInt32(pc.NextValue() / 1024); if (i > count[8]) { count[8] = i; } } pc = new PerformanceCounter("Process", "Page File Bytes", "_Total"); //页面文件使用量 count[12] = Convert.ToInt32(pc.NextValue() / 1024); } private void Form1_Load(object sender, EventArgs e) { label4.Text = ""; label5.Text = ""; label6.Text = ""; label10.Text = ""; label11.Text = ""; label12.Text = ""; label16.Text = ""; label17.Text = ""; label18.Text = ""; label22.Text = ""; label23.Text = ""; label24.Text = ""; label25.Text = ""; label30.Text = ""; progressBar1.Value = 0; timer1.Interval = 1000; timer1.Enabled = true; } private void timer1_Tick(object sender, EventArgs e) { RefData(); label4.Text = count[0].ToString(); label5.Text = count[1].ToString(); label6.Text = count[2].ToString(); label10.Text = count[3].ToString(); label11.Text = count[4].ToString(); label12.Text = count[5].ToString(); label16.Text = count[6].ToString(); label17.Text = count[7].ToString(); label18.Text = count[8].ToString(); label22.Text = count[9].ToString(); label23.Text = count[10].ToString(); label24.Text = count[11].ToString(); label25.Text = (count[12] / 1024).ToString() + "MB"; progressBar1.Value = Convert.ToInt32(count[12] / 1024.0 / 2048.0 * 100); label30.Text = label25.Text + "/2048MB"; } } } /*CPU使用率的问题: PerformanceCounter("Process","% Processor Time","_Total"); PerformanceCounter("Processor","% Processor Time","_Total"); 上面两句来得到的CPU使用率时时为0,为何?(XP SP3系统,难道是不支持?)请问应该怎么写?还是有其他间接的方法? ****************/