Jump to content

Getting a process's cpu usage


Recommended Posts

I have this function and the cpu usage always comes out to 0 even when the process is at like 10+

Here is the code (UpdateCPUToolTip() just updates the tray icons tooltip):

Most of it is from here

Func UpdateCPU()    
    Local $objWMIService = ObjGet("winmgmts:\\" & @ComputerName & "\root\CIMV2")
    
    $CPU = "ERROR"
    
    If(@error) Then
        UpdateCPUToolTip()
        Return
    EndIf
    
    $colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_PerfRawData_PerfProc_Process WHERE IDProcess = '" & $PID & "'")
    
    For $objItem In $colItems
        $iT1 = $objItem.TimeStamp_Sys100NS
        $iP1 = $objItem.PercentProcessorTime
        
        If($objItem = "") Then
            UpdateCPUToolTip()
            Return
        EndIf
        
        Sleep(300)
    Next
    
    $colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_PerfRawData_PerfProc_Process WHERE IDProcess = '" & $PID & "'")
    For $objItem In $colItems
        $iP2 = $objItem.PercentProcessorTime
        $iT2 = $objItem.TimeStamp_Sys100NS
    Next
    
    If($objItem = "") Then
        UpdateCPUToolTip()
        Return
    EndIf
    
    $iPP = ($iP2 - $iP1)
    $iTT = ($iT2 - $iT1)
    
    If($iTT = 0) Then
        $CPU = 100
    Else
        $CPU = round(($iPP/$iTT) * 100, 0)
    EndIf
    
    UpdateCPUToolTip()
EndFunc

Is there anything wrong with this code?

Link to comment
Share on other sites

It seems to only return 0 and 100

I'm using

$colItems = ""
    $objWMIService = ObjGet("winmgmts:\\" & @ComputerName & "\root\CIMV2")
    $colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_PerfFormattedData_PerfProc_Process WHERE IDProcess = '" & $PID & "'", "WQL", 0x30)
    
    If(IsObj($colItems)) Then
        For $objItem In $colItems
           $CPU = $objItem.PercentProcessorTime
           MsgBox(64, "$CPU", $CPU)
        Next
    EndIf
Edited by Spacetech
Link to comment
Share on other sites

  • Moderators

It seems to only return 0 and 100

I'm using

$colItems = ""
    $objWMIService = ObjGet("winmgmts:\\" & @ComputerName & "\root\CIMV2")
    $colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_PerfFormattedData_PerfProc_Process WHERE IDProcess = '" & $PID & "'", "WQL", 0x30)
    
    If(IsObj($colItems)) Then
        For $objItem In $colItems
           $CPU = $objItem.PercentProcessorTime
           MsgBox(64, "$CPU", $CPU)
        Next
    EndIf
Why not just run the example as it is written, I can already tell just from looking at this snippet you are missing at least one value.

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...