Reputation: 57
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?
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
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