I am writing a program that fuctions pretty much like the windows task manager. I am able to list all the running processes and get the pids.
The problem im having is when it comes to getting the cpu and memory usage of each process. I just cant find out how to get this information.
Could anyone provide me with a link or some code on how to get it?
I found this article once
( http://www.alexfedotov.com/samples/wmitop.asp )
I think it will help you, to understand how to calculate the CPU
usage. I also wrote a small (it's a laity one) program that shows
the CPU usage of the program itself. I hope this will help.
Note that my calculations are actually wrong, because i only use
the Low part of the DateTime structure. Normally and correctly
one should use the structure as a 64-bit Value. For more
information look in MSDN ( GetProcessTime)
typecast aka Pavel
source code of the program:
( i use default names. one form with one timer and one label)
Private Type FILETIME
dwLowDateTime As Long
dwHighDateTime As Long
Private Declare Function GetProcessTimes Lib "kernel32" (ByVal hProcess As Long, lpCreationTime As FILETIME, lpExitTime As FILETIME, lpKernelTime As FILETIME, lpUserTime As FILETIME) As Long
Private Declare Function GetCurrentProcess Lib "kernel32" () As Long
Private bRet As Boolean
Private ftCreat As FILETIME
Private ftExit As FILETIME
Private ftKernel As FILETIME
Private ftUser As FILETIME
Private Sub Form_Load()
bRet = False
Private Sub Timer1_Timer()
Static ftKernelStart As FILETIME
Static ftUserStart As FILETIME
Static bNotOnce As Boolean
bRet = GetProcessTimes(GetCurrentProcess, ftCreat, ftExit, ftKernel, ftUser)
If bRet And bNotOnce Then
Dim PU As Double
PU = ((ftKernel.dwLowDateTime - ftKernelStart.dwLowDateTime) + (ftUser.dwLowDateTime - ftUserStart.dwLowDateTime) / 1000) * 100
PU = PU / 10000000
Label1.Caption = CStr(PU)
bNotOnce = True
ftKernelStart = ftKernel
ftUserStart = ftUser
Cheers for that code, it took me a while but i finaly got it working.
Only problem i have now is i dont know how to work with the two 32 bit numbers. How do i get the correct value out of them in that calculation?