C# 使用C获得特定的处理器CPU使用率#

C# 使用C获得特定的处理器CPU使用率#,c#,cpu-usage,processor,C#,Cpu Usage,Processor,我在Google上找到了下面的代码,以获得特定的处理器CPU使用率 int processorCount = Environment.ProcessorCount; PerformanceCounter myAppCpu = new PerformanceCounter( "Process", "% Processor Time", "chrome", true); float cpuUsage = myAppCpu.NextValue() / processorCount; 但在我的Task

我在Google上找到了下面的代码,以获得特定的处理器CPU使用率

int processorCount = Environment.ProcessorCount;
PerformanceCounter myAppCpu = new PerformanceCounter( "Process", "% Processor Time", "chrome", true);
float cpuUsage = myAppCpu.NextValue() / processorCount;
但在我的TaskManager中,一个磁盘映像可能有多个PID


所以,我想知道,这段代码计算的是Chrome浏览器的CPU使用量吗?

您可以使用kernel32.dll中的GetProcessTimes函数。 请参见此处的.NET用法:

第一个参数是进程的句柄。使用
System.Diagnostics.Process.GetCurrentProcess().Handle
System.Diagnostics.Process.GetProcessById(123).Handle
或任何其他流程实例

使用系统;
使用System.Collections.Generic;
使用系统组件模型;
使用System.Linq;
使用System.Runtime.InteropServices;
使用System.Runtime.InteropServices.ComTypes;
使用系统文本;
使用系统线程;
使用System.Threading.Tasks;
命名空间CPUUsage
{
班级计划
{
静态void Main(字符串[]参数)
{
var状态=新的CPUStatus(System.Diagnostics.Process.GetCurrentProcess());
WriteLine(“CPU使用率:{0}”,status.RawUsage);
var processs=System.Diagnostics.Process.getProcessByName(“devenv”);
if(processs.Any())
{
var status2=新的CPUStatus(processs.First());
WriteLine(“Devenv CPU使用率:{0}”,status.RawUsage);
睡眠(1000);
WriteLine(“Devenv CPU使用率:{0}”,status.RawUsage);
}
}
}
/// 
///类来检索CPU值。
/// 
/// 
公共类CPUStatus
{
#区域“成员”
private ProcessTimes_ProcessTimes=new ProcessTimes();
私有长用户时间;
私人长时间;
私有日期时间_OldUpdate;
私人Int32_使用;
私有对象_Lock=新对象();
私有IntPtr\u进程句柄;
#端区
#区域“构造函数”
/// 
///初始化CPUStatus实例
/// 
///监控过程
公共CPUStatus(系统诊断过程)
{
_OldUpdate=DateTime.MinValue;
_processHandle=process.Handle;
InitValues();
}
#端区
#地区进口
[DllImport(“kernel32.dll”,SetLastError=true)]
[返回:Marshallas(UnmanagedType.Bool)]
静态外部bool GetProcessTimes(IntPtr hProcess,out long lpCreationTime,out long lpExitTime,out long lpKernelTime,out long lpUserTime);
#端区
#区域“私有方法”
/// 
///检索初始值
/// 
/// 
私有void InitValues()
{
尝试
{
if((GetProcessTimes(_processHandle,out _ProcessTimes.RawCreationTime,out _ProcessTimes.RawExitTime,out _ProcessTimes.RawKernelTime,out _ProcessTimes.RawUserTime)))
{
//将值转换为日期时间值
_ProcessTimes.ConvertTime();
_OldUserTime=\u ProcessTimes.UserTime.Ticks;
_OldKernelTime=\u ProcessTimes.KernelTime.Ticks;
_OldUpdate=DateTime.Now;
}
}
捕获(例外)
{
_OldUpdate=DateTime.MinValue;
}
}
/// 
///刷新使用值
/// 
/// 
私有无效刷新()
{
锁
{
if((GetProcessTimes(_processHandle,out _ProcessTimes.RawCreationTime,out _ProcessTimes.RawExitTime,out _ProcessTimes.RawKernelTime,out _ProcessTimes.RawUserTime)))
{
//将值转换为日期时间值
_ProcessTimes.ConvertTime();
UpdateCPUUsage(_ProcessTimes.UserTime.Ticks,_ProcessTimes.KernelTime.Ticks);
}
其他的
{
抛出新的Win32Exception(Marshal.GetLastWin32Error(),“无法检索进程时间”);
}
}
}
/// 
///更新cpu使用率(cpu usgae=UserTime+KernelTime)
/// 
/// 
/// 
/// 
私有void UpdateCPUUsage(长newUserTime、长newKernelTime)
{
长UpdateDelay=0;
long UserTime=newUserTime-\u OldUserTime;
long KernelTime=newKernelTime-_OldKernelTime;
如果(_OldUpdate==DateTime.MinValue)
{
_RawUsage=Convert.ToInt32((用户时间+内核时间)*100);
}
其他的
{
//消除“除以零”
if(DateTime.Now.Ticks==\u OldUpdate.Ticks)
睡眠(100);
UpdateDelay=DateTime.Now.Ticks-\u OldUpdate.Ticks;
_RawUsage=Convert.ToInt32(((UserTime+KernelTime)*100)/UpdateDelay);
}
_OldUserTime=newUserTime;
_OldKernelTime=newKernelTime;
_OldUpdate=DateTime.Now;
}
#端区
#区域“财产”
/// 
///获取CPU使用率
/// 
/// 
/// 
/// 
公共IntrawUsage
{
得到
{
锁
{
刷新();
返回使用;
}
}
}
#端区
#区域“内部类”
私有结构处理时间
{
公共日期时间创建时间;
公共日期时间出口时间;
公共日期时间内核时间;
公共日期时间用户时间;
公众参与时间长;
公共长时间;
公共长核时间;
公众参与时间长;
公共时间()
{
CreationTime=FiletimeToDateTime(RawCreationTime);
ExitTime=FiletimeToDateTime(rawesitTime);
KernelTime=FiletimeToDateTime(RawKernelTime);
UserTime=FiletimeToDateTime(RawUserTime);
}
专用日期时间FiletimeToDateTime(长文件时间)
{
尝试
{
返回日期时间。FromFileTimeUtc(FileTime);
}
捕获(例外)
{
返回新的D