取单个进程的cpu使用率,要代码能调试。本人做过不过取的不准确求准确的做法

解决方案 »

  1.   

    .NET中有一个System.Diagnostics包,其中的PerformanceCounter和PerformanceCounterCategory可以帮助遍历性能信息。PerformanceCounterCateogory.GetCategories() 返回一个PerformanceCounterCategory数组,其中CategoryName为”Processor“的 PerformanceCounterCategory,包含了CPU占用率的信息。PerformanceCounterCategory category = new PerformanceCounterCategory("Processor");
    string[] instances = category.GetInstanceNames();一段代码中,如果是双CPU电脑,会返回{"_Total", "0", "1"}三个数组。其中”_Total"永远是总的占用率,而其他数字代表CPU内核。
      

  2.   

    谢谢csrwgs,不过不是cpu总使用率,而是单个进程的。
      

  3.   

    csrwgs 我再看下。我有个这个解法的demo原来没考虑双核。如果我试验成功的话会把分加上
      

  4.   

            Timer tmT;
            int iTotal, iUser;        public Form1()
            {
                InitializeComponent();
            }        private void Form1_Load(object sender, EventArgs e)
            {
                tmT = new Timer();
                tmT.Tick += new EventHandler(tmT_Tick);
                tmT.Interval = 1000;
                tmT.Start();
            }        void tmT_Tick(object sender, EventArgs e)
            {
                int I = System.Diagnostics.Process.GetCurrentProcess().TotalProcessorTime.Milliseconds;
                System.Console.WriteLine("{0:P}",
                    (I - iTotal) / 1000.0);
                iTotal = I;
                I = System.Diagnostics.Process.GetCurrentProcess().UserProcessorTime.Milliseconds;
                System.Console.WriteLine("{0:P}",
                    (I - iUser) / 1000.0);
                iUser = I;
            }