不断积累,必然飞跃,突破随之!

相信自己,开拓生活!
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

获取cpu使用率,获取某一进程cpu使用率

Posted on 2010-11-27 11:13  Tangyuan2017  阅读(864)  评论(0编辑  收藏  举报
using System;
using System.Diagnostics;

namespace MyConsole
{
    
class Program
    {
        
static void Main(string[] args)
        {
            
string filePath = "E:\\render\\bin\\render.exe";
            
string argument = "\\\\192.168.12.166\\ceshi_Project\\scenes\\ceshi.mb";
            
try
            {
                Process p 
= Process.Start(filePath, argument);
                p.PriorityClass 
= ProcessPriorityClass.BelowNormal;
                
//获取cpu使用率
                PerformanceCounter cpuUsage = new PerformanceCounter("Processor""% Processor Time""_Total");

                
//获取某一进程cpu使用率
                PerformanceCounter renderUsage = new PerformanceCounter("Process""% Processor Time", p.ProcessName);
                
do
                {
                    Console.WriteLine(
"---------------------");
                    Console.WriteLine(
"总使用率:{0:p1}", cpuUsage.NextValue() / 100);
                    Console.WriteLine(
"{1}使用率:{0:p1}", renderUsage.NextValue() / 100, p.ProcessName);
                    System.Threading.Thread.Sleep(
1000);
                }
                
while (true);
            }
            
catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
                Console.Read();
            }
        }
    }
}