Process CPU/Mem Usage
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: Process CPU/Mem Usage

Hybrid View

  1. #1
    Join Date
    Jan 2003
    Posts
    9

    Process CPU/Mem Usage

    Hi all,

    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?

    Regards,
    Benneb

  2. #2
    Join Date
    Apr 2004
    Posts
    17

    Post Process CPU / Mem usage

    Hi,
    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)

    Regards,
    typecast aka Pavel

    source code of the program:
    ( i use default names. one form with one timer and one label)

    Code:
    Option Explicit
    
    Private Type FILETIME
        dwLowDateTime As Long
        dwHighDateTime As Long
    End Type
    
    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
    End Sub
    
    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)
        Else
            bNotOnce = True
        End If
        
        ftKernelStart = ftKernel
        ftUserStart = ftUser
    End Sub

  3. #3
    Join Date
    Jan 2003
    Posts
    9
    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?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center