Kabundscha
Kabundscha

Reputation: 57

PerformanceCounter Datas not correct?

i have a question... I get the actual CPU&Ram usage from PerfomanceCounter, but the result in RAM/CPU looks very different in task manager, why?

left taskmanager, right form1 enter image description here

thats my code for cpu&ram usage for my Form1..

    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";
    }

my timer ticks in 500ms:

    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.###")) + " %");
    }

//Working Stuff...

private PerformanceCounter theMemCounter = new PerformanceCounter("Process", "Working Set - Private", Process.GetCurrentProcess().ProcessName);

Upvotes: 1

Views: 104

Answers (1)

Mihir Dave
Mihir Dave

Reputation: 4024

That's because you are getting performance count of your current process

private PerformanceCounter theCPUCounter = new PerformanceCounter("Process", "% Processor Time", Process.GetCurrentProcess().ProcessName);

Process.GetCurrentProcess().ProcessName

so if you want to get the overall Usage Check this

Upvotes: 1

Related Questions