首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >PerformanceCounter数据不正确吗?

PerformanceCounter数据不正确吗?
EN

Stack Overflow用户
提问于 2018-06-29 19:53:11
回答 1查看 81关注 0票数 1

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

左边的任务管理器,右边的form1

这是我的Form1的cpu和ram使用率的代码。

代码语言:javascript
复制
    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毫秒:

代码语言:javascript
复制
    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.###")) + " %");
    }

//工作内容...

代码语言:javascript
复制
private PerformanceCounter theMemCounter = new PerformanceCounter("Process", "Working Set - Private", Process.GetCurrentProcess().ProcessName);
EN

回答 1

Stack Overflow用户

发布于 2018-06-29 20:10:56

这是因为您正在获取当前进程的性能计数

代码语言:javascript
复制
private PerformanceCounter theCPUCounter = new PerformanceCounter("Process", "% Processor Time", Process.GetCurrentProcess().ProcessName);

Process.GetCurrentProcess().ProcessName

因此,如果您想获取总体使用情况,请使用Check this

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51101151

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档