• C

Having a Program Monitor Its Own CPU Usage

I am looking for a way to have a program monitor its own CPU usage.  Can anyone describe how I may do this (code would be appreciated as well)?  I am trying to write the program in C using Microsoft Visual .Net.  Thanks.
jtradesAsked:
Who is Participating?
 
x4uCommented:
You can call GetCurrentProcess() to get your your own process handle and then use GetProcessTimes() to find out how much time was spent by your process.
http://msdn.microsoft.com/library/en-us/dllproc/base/getprocesstimes.asp

If you want to get a percentage value similar to those displayed in task manager you can use the Performance Data Helper API:
http://support.microsoft.com/kb/262938/en-us
There is a discusssion about this and a code sample at the end of this articte:
http://www-128.ibm.com/developerworks/websphere/techjournal/0310_braithwaite/braithwaite.html
see here how to get the value with PdhGetFormattedCounterValue directly:
http:Q_20085897.html
0
 
Prashant SabnekarAVPCommented:
>>>jtrades, if you want it in Linux i can give you. But for windows I should try.

All the best
Prashant Sabnekar
 
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.