c# 获取某个进程的CPU使用百分百(类似任务管理器中显示CPU)

简介:

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Management;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace 进程监控
{
class Program
{
static void Main(string[] args)
{
Process[] processes = Process.GetProcessesByName("taskmgr");

foreach (Process instance in processes)
{
Console.WriteLine("");
Console.WriteLine("ProcessName:" + instance.ProcessName);
try
{
//Console.WriteLine("提交大小\t" + instance.PagedMemorySize64 / 1024);
Console.WriteLine("工作设置(内存)\t" + instance.WorkingSet64 / 1024);
Console.WriteLine("线程数\t" + instance.Threads.Count);
Console.WriteLine("句柄数\t" + instance.HandleCount);

}
catch { }
}

Process p = processes[1];
//PerformanceCounter ramCounter = new PerformanceCounter("Process", "Working Set", p.ProcessName);
//PerformanceCounter cpuCounter = new PerformanceCounter("Process", "% Processor Time", p.ProcessName);


var objQuery = new ObjectQuery("select * from Win32_Process WHERE ProcessID = " + p.Id);
var moSearcher = new ManagementObjectSearcher(objQuery);
DateTime firstSample = DateTime.MinValue, secondSample = DateTime.MinValue;

double ProcessorUsage;
double msPassed;
ulong u_OldCPU = 0;
while (true)
{
var gets = moSearcher.Get();
foreach (ManagementObject mObj in gets)
{
try
{
if (firstSample == DateTime.MinValue)
{
firstSample = DateTime.Now;
mObj.Get();
u_OldCPU = (ulong)mObj["UserModeTime"] + (ulong)mObj["KernelModeTime"];
}
else
{
secondSample = DateTime.Now;
mObj.Get();
ulong u_newCPU = (ulong)mObj["UserModeTime"] + (ulong)mObj["KernelModeTime"];

msPassed = (secondSample - firstSample).TotalMilliseconds;
ProcessorUsage = (u_newCPU - u_OldCPU) / (msPassed * 100.0 * Environment.ProcessorCount);

u_OldCPU = u_newCPU;
firstSample = secondSample;
Console.WriteLine("ProcessorUsage:" + (int)ProcessorUsage);
}

}
catch (Exception ex)
{
Console.WriteLine(ex.Message + ex.StackTrace);
Console.WriteLine(ex.InnerException.Message);
}
}
Thread.Sleep(1000);
}
Console.ReadLine();
}
}
}




本文转自94cool博客园博客,原文链接:http://www.cnblogs.com/94cool/p/6336593.html,如需转载请自行联系原作者 

相关文章
|
2月前
|
存储 监控 算法
电脑监控管理中的 C# 哈希表进程资源索引算法
哈希表凭借O(1)查询效率、动态增删性能及低内存开销,适配电脑监控系统对进程资源数据的实时索引需求。通过定制哈希函数与链地址法冲突解决,实现高效进程状态追踪与异常预警。
163 10
|
9月前
|
网络协议 前端开发 调度
schedule:原来还可以这样让进程让出 CPU?
文章主要讲述通过模拟时钟中断和调度事件来优化和测试虚拟机监控器(VMM)的方法,包括流程设计、寄存器状态的保存与恢复、硬件中断处理规范等细节。
226 11
schedule:原来还可以这样让进程让出 CPU?
|
Windows
dllhost.exe进程占用CPU很高怎么解决?
全面解析dllhost.exe进程
1106 16
|
12月前
|
Java 对象存储 开发者
如何找出Java进程占用CPU高的元凶
本文记录了一次Java进程CPU占用率过高的问题和排查思路。
|
存储 监控
【Azure Cloud Service】在Azure云服务中收集CPU监控指标和IIS进程的DUMP方法
在使用Cloud Service服务时,发现服务的CPU占用很高,在业务请求并不大的情况下,需要直到到底是什么进程占用了大量的CPU资源,已经如何获取IIS进程(w3wp.exe)的DUMP文件?
159 8
|
弹性计算 Linux 区块链
Linux系统CPU异常占用(minerd 、tplink等挖矿进程)
Linux系统CPU异常占用(minerd 、tplink等挖矿进程)
480 4
Linux系统CPU异常占用(minerd 、tplink等挖矿进程)
|
C# 开发工具 Windows
C# 获取Windows系统信息以及CPU、内存和磁盘使用情况
C# 获取Windows系统信息以及CPU、内存和磁盘使用情况
468 0
|
Linux Python
在Linux中,如何查找系统中占用CPU最高的进程?
在Linux中,如何查找系统中占用CPU最高的进程?
在Linux中,如何查看占用CPU最多的进程?
在Linux中,如何查看占用CPU最多的进程?
|
安全 API C#
C# 如何让程序后台进程不被Windows任务管理器强制结束
C# 如何让程序后台进程不被Windows任务管理器强制结束
535 0

热门文章

最新文章