Click here to Skip to main content
15,946,525 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
In my application i want to display cpu and memory usage in every time intervel of 1 second. Please help me to get exact details.

What I have tried:

iam using PerformanceCounter to get this information, but it is not correct with cpu usage of system. Please find the the code that are used in my application.

PerformanceCounter cpuCounter;
cpuCounter = new PerformanceCounter("Processor", "% Privileged Time", "_Total");
lbl_cpu_usage.Text = Convert.ToInt32(cpuCounter.NextValue()).ToString() + "%";(This code is written in the timer tick event.
Updated 1-Mar-17 19:42pm

1 solution

Performance counter objects in general need two values sampled at 1 - 2 seconds apart to be able to give accurate readings .. although I hate the fact I'm using a thread delay here, I can't think of a simpler way - see the two lines in bold I've added to your code - see if that makes it more accurate

PerformanceCounter cpuCounter;
cpuCounter = new PerformanceCounter("Processor", "% Privileged Time", "_Total");
lbl_cpu_usage.Text = Convert.ToInt32(cpuCounter.NextValue()).ToString() + "%";

[edit] ref : Performance Counter Value Retrieval[^] [/edit]
Share this answer
Rahul s menon 2-Mar-17 1:45am    
I need to update cpu and memory usage in every 1 seconds, that's why i am using timer. The above code is not given the exact usage detail.
Graeme_Grant 2-Mar-17 1:50am    
use a timer set to 1000 milliseconds and don't use Thread.Sleep.
Rahul s menon 2-Mar-17 1:54am    
I am already timer is used with an time intervel of 1000 milliseconds. But both cpu and memory percentage is different from system usage.
Garth J Lancaster 2-Mar-17 1:58am    
how 'different' ? - I don't think you're ever going to get figures that exactly match the system usage
Rahul s menon 2-Mar-17 2:07am    
we can get exact usage by implementing wmi. if you have any idea to implement wmi?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900