创建一个控制台应用程序,代码如下
![](https://yqfile.alicdn.com/img_1c53668bcee393edac0d7b3b3daff1ae.gif)
![](https://yqfile.alicdn.com/img_405b18b4b6584ae338e0f6ecaf736533.gif)
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 );
}
}
}
}
}
}
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 );
}
}
}
}
}
}
![img_fa0be433d68c8212b2b0b3b1a564ccb1.png](https://yqfile.alicdn.com/img_fa0be433d68c8212b2b0b3b1a564ccb1.png?x-oss-process=image/resize,w_1400/format,webp)
如果本文对你有所帮助,请打赏——1元就足够感动我:)
联系邮箱:intdb@qq.com
我的GitHub: https://github.com/vipstone
![支付宝打赏 支付宝打赏](https://yqfile.alicdn.com/img_c4c57b8d4989a9d91e4dbe075ca4974a.png?x-oss-process=image/resize,w_1400/format,webp)
![微信打赏 微信打赏](https://yqfile.alicdn.com/img_c8b71247e0b11cfa3c1502d9f360a666.png?x-oss-process=image/resize,w_1400/format,webp)
联系邮箱:intdb@qq.com
我的GitHub: https://github.com/vipstone
关注公众号:
![img_9bde0f31ac4a0eca10b1bd7414b78faf.png](https://yqfile.alicdn.com/img_9bde0f31ac4a0eca10b1bd7414b78faf.png?x-oss-process=image/resize,w_1400/format,webp)
作者: 王磊
出处: http://vipstone.cnblogs.com/
本文版权归作者和博客园共有,欢迎转载,请标明出处。