创建一个控制台应用程序,代码如下

 

代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

//引用2命名空间
using System.Diagnostics;
using System.Threading;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Process[] p
= Process.GetProcessesByName("devenv");//获取指定进程信息
// Process[] p = Process.GetProcesses();//获取所有进程信息
string cpu = string.Empty;
string info = string.Empty;

PerformanceCounter pp
= new PerformanceCounter();//性能计数器
pp.CategoryName = "Process";//指定获取计算机进程信息 如果传Processor参数代表查询计算机CPU
pp.CounterName = "% Processor Time";//占有率
//如果pp.CategoryName="Processor",那么你这里赋值这个参数 pp.InstanceName = "_Total"代表查询本计算机的总CPU。
pp.InstanceName = "devenv";//指定进程
pp.MachineName = ".";
if (p.Length > 0)
{
foreach (Process pr in p)
{
while (true)//1秒钟读取一次CPU占有率。
{
info
= pr.ProcessName + "内存:" +
(Convert.ToInt64(pr.WorkingSet64.ToString())
/ 1024).ToString();//得到进程内存
Console.WriteLine(info + " CPU使用情况:" + Math.Round(pp.NextValue(), 2).ToString() + "%");
Thread.Sleep(
1000);
}
}
}
}
}
}

 

相关文章:

  • 2021-07-06
  • 2022-01-09
  • 2021-12-28
  • 2021-06-29
  • 2021-09-23
  • 2022-12-23
  • 2021-09-30
  • 2021-05-04
猜你喜欢
  • 2021-09-06
  • 2021-07-27
  • 2022-12-23
  • 2022-12-23
  • 2022-02-18
  • 2022-12-23
  • 2021-12-28
相关资源
相似解决方案