我有个问题..。我从PerfomanceCounter得到了实际的CPU和内存使用率,但是在任务管理器中的结果看起来很不一样,为什么?
左边的任务管理器,右边的form1

这是我的Form1的cpu和ram使用率的代码。
private PerformanceCounter theCPUCounter = new PerformanceCounter("Process", "% Processor Time", Process.GetCurrentProcess().ProcessName);
private PerformanceCounter theMemCounter = new PerformanceCounter("Process", "Working Set", Process.GetCurrentProcess().ProcessName);
private String getCpuUsagePercent()
{
string abc = Convert.ToString(theCPUCounter.NextValue().ToString("##0.##"));
return abc + " %";
}
private String getramusage()
{
return theMemCounter.NextValue() + " MB";
}我的计时器计时500毫秒:
private void timer1_Tick(object sender, EventArgs e)
{
double ram = theMemCounter.NextValue();
double cpu = theCPUCounter.NextValue();
double cpucalc = ram / 1024 / 1024;
string cpucalc1 = Convert.ToString(cpucalc.ToString("######0.###"));
label37.Text = ("RAM: " + cpucalc1 + " MB; CPU: " + (cpu.ToString("###0.###")) + " %");
}//工作内容...
private PerformanceCounter theMemCounter = new PerformanceCounter("Process", "Working Set - Private", Process.GetCurrentProcess().ProcessName);发布于 2018-06-29 20:10:56
这是因为您正在获取当前进程的性能计数
private PerformanceCounter theCPUCounter = new PerformanceCounter("Process", "% Processor Time", Process.GetCurrentProcess().ProcessName);Process.GetCurrentProcess().ProcessName
因此,如果您想获取总体使用情况,请使用Check this
https://stackoverflow.com/questions/51101151
复制相似问题