However, I think that we are fundamentally flawed by trying to determine process times from within the process itself. We are loading the display thread with more work; Heisenberg strikes again. <smile>
I think that GetProcessTimes is one way to go but that should really be called by a separate process.
Re your code.
I don't like the look of (sys-idl)*100/sys
I would forget about idl and consider the ratio of sys and real time. The real time would be the difference between the current time got just before calling GetSystemTimes in the current instance of the cpu_usage function and the previous instance of the cpu_usage function. This ratio would then give you a CPU load.
With regard real time we could use something like
Code: Select all
RealTime = CurrentTime - LastCurrentTime
Code: Select all
Dim As Longint CurrentTime
GetSystemTimeAsFileTime( @CurrentTime )
Added: GetProcessTimes is a bit of a pain - it wants the process handle of the target process. It we use OpenProcess and give that the process id of the target process then we can get the handle. We can get the process id of the target process from Task Manager. Never easy, is it?