Jump to content

Get CPU usage of a process


Recommended Posts

Hi,

as you probably know, you can view the processes with Ctrl Alt Delete and you can see the CPU usage of every process there, is it possible in AutoIt to get the CPU usage of a process?

I know the 'Process'-functions like:

ProcessClose

ProcessExists

_ProcessGetName

_ProcessGetPriority

ProcessList

ProcessSetPriority

ProcessWait

ProcessWaitClose

But they don't give me the CPU usage :">

How can i get the CPU usage of a single process?

I have to go to bed now, so i'll reply tomorrow :D

Programs so far:Teh Serializer - Search for licenses for Nero - Windows - Office - Alcohol etc.
Link to comment
Share on other sites

or maybe?

While 1
    Sleep(10)
    $cpu_usage = _Get_CPU_Usage(@ComputerName, 1, "taskmgr"); return usages if above the percent passed in
    If Not @error Then
        MsgBox(0, "CPU Usage", $cpu_usage)
        ExitLoop
    EndIf
WEnd

Func _Get_CPU_Usage($strComputer, $percent = 2, $strProcess = "")
    $wbemFlagReturnImmediately = 0x10
    $wbemFlagForwardOnly = 0x20
    $colItems = ""
    
    If $strProcess <> "" Then $strProcess = " WHERE Name = '" & $strProcess & "'"
    $objWMIService = ObjGet("winmgmts:\\" & $strComputer & "\root\CIMV2")
    $colItems = $objWMIService.ExecQuery ("SELECT * FROM Win32_PerfFormattedData_PerfProc_Process" & $strProcess, "WQL", _
            $wbemFlagReturnImmediately + $wbemFlagForwardOnly)
    
    If IsObj($colItems) Then
        For $objItem In $colItems
            If Number($objItem.PercentProcessorTime) > Number($percent) Then Return $objItem.PercentProcessorTime
        Next
    EndIf
    SetError(1)
    Return ""
EndFunc  ;==>_Get_CPU_Usage

SciTE for AutoItDirections for Submitting Standard UDFs

 

Don't argue with an idiot; people watching may not be able to tell the difference.

 

Link to comment
Share on other sites

or maybe?

While 1
    Sleep(10)
    $cpu_usage = _Get_CPU_Usage(@ComputerName, 1, "taskmgr"); return usages if above the percent passed in
    If Not @error Then
        MsgBox(0, "CPU Usage", $cpu_usage)
        ExitLoop
    EndIf
WEnd

Func _Get_CPU_Usage($strComputer, $percent = 2, $strProcess = "")
    $wbemFlagReturnImmediately = 0x10
    $wbemFlagForwardOnly = 0x20
    $colItems = ""
    
    If $strProcess <> "" Then $strProcess = " WHERE Name = '" & $strProcess & "'"
    $objWMIService = ObjGet("winmgmts:\\" & $strComputer & "\root\CIMV2")
    $colItems = $objWMIService.ExecQuery ("SELECT * FROM Win32_PerfFormattedData_PerfProc_Process" & $strProcess, "WQL", _
            $wbemFlagReturnImmediately + $wbemFlagForwardOnly)
    
    If IsObj($colItems) Then
        For $objItem In $colItems
            If Number($objItem.PercentProcessorTime) > Number($percent) Then Return $objItem.PercentProcessorTime
        Next
    EndIf
    SetError(1)
    Return ""
EndFunc ;==>_Get_CPU_Usage
Strange, for some reason the returned value is not the same that task manager shows...or is it just me :D

Alzo

Don't take my pic to serious...~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~You Looked, but you did not see!~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Link to comment
Share on other sites

or maybe?

While 1
    Sleep(10)
    $cpu_usage = _Get_CPU_Usage(@ComputerName, 1, "taskmgr"); return usages if above the percent passed in
    If Not @error Then
        MsgBox(0, "CPU Usage", $cpu_usage)
        ExitLoop
    EndIf
WEnd

Func _Get_CPU_Usage($strComputer, $percent = 2, $strProcess = "")
    $wbemFlagReturnImmediately = 0x10
    $wbemFlagForwardOnly = 0x20
    $colItems = ""
    
    If $strProcess <> "" Then $strProcess = " WHERE Name = '" & $strProcess & "'"
    $objWMIService = ObjGet("winmgmts:\\" & $strComputer & "\root\CIMV2")
    $colItems = $objWMIService.ExecQuery ("SELECT * FROM Win32_PerfFormattedData_PerfProc_Process" & $strProcess, "WQL", _
            $wbemFlagReturnImmediately + $wbemFlagForwardOnly)
    
    If IsObj($colItems) Then
        For $objItem In $colItems
            If Number($objItem.PercentProcessorTime) > Number($percent) Then Return $objItem.PercentProcessorTime
        Next
    EndIf
    SetError(1)
    Return ""
EndFunc ;==>_Get_CPU_Usage
Thank you for making it / searching it, but that seems to complicated for me, want i wanted to make is a 'Process Bar' which inidicates how much CPU my program is using, maybe i take a look at this later, thank you all for now! :D
Programs so far:Teh Serializer - Search for licenses for Nero - Windows - Office - Alcohol etc.
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...