Jump to content
Sign in to follow this  
Zohar

[Closed] Problem in Getting a Process's CPU Usage

Recommended Posts

Zohar

I am trying to get the CPU Usage of a Single process,

and the values I get are not any value between 0 and 100,

but only these values:

0,25,50,75,100

Why is that?

Local   $wbemFlagReturnImmediately = 0x10
Local   $wbemFlagForwardOnly = 0x20


While 1
    ConsoleWrite(_Get_CPU_Usage("emule") & ",")
    Sleep(500)
WEnd



Func _Get_CPU_Usage($strProcess)
    Local   $objWMIService = ObjGet("winmgmts:\\" & @ComputerName & "\root\CIMV2")
    Local   $colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_PerfFormattedData_PerfProc_Process WHERE Name = '"&$strProcess&"'","WQL",$wbemFlagReturnImmediately+$wbemFlagForwardOnly)
    

    For $objItem In $colItems
        Beep(2000,100)
        Return $objItem.PercentProcessorTime

        ;for now, I'm returning the result from the first item in the collection only.
        ;(why would that collection ever be bigger than 1?)
        ;(and why when I do $colItems.item(0) it fails?)
    Next
EndFunc
Edited by Zohar

Share this post


Link to post
Share on other sites
PsaltyDS

I am trying to get the CPU Usage of a Single process,

and the values I get are not any value between 0 and 100,

but only these values: 0,25,50,75,100

Why is that?

Local   $wbemFlagReturnImmediately = 0x10
Local   $wbemFlagForwardOnly = 0x20

While 1
    ConsoleWrite(_Get_CPU_Usage("emule") & ",")
    Sleep(500)
WEnd

Func _Get_CPU_Usage($strProcess)
    Local   $objWMIService = ObjGet("winmgmts:\\" & @ComputerName & "\root\CIMV2")
    Local   $colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_PerfFormattedData_PerfProc_Process WHERE Name = '"&$strProcess&"'","WQL",$wbemFlagReturnImmediately+$wbemFlagForwardOnly)

    For $objItem In $colItems
        Beep(2000,100)
        Return $objItem.PercentProcessorTime

    ;for now, I'm returning the result from the first item in the collection only.
    ;(why would that collection ever be bigger than 1?)
    ;(and why when I do $colItems.item(0) it fails?)
    Next
EndFunc
You need an SWbemRefresher object for time-based counters. Look at the method used for _ProcessListProperties(), but also note the memory leak in Microsoft's function, which is discussed in the same topic.

:P


Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share this post


Link to post
Share on other sites
Zohar

thank you PsaltyDS..

If it leaks, and I do need to use it repeatedly,

what else can I use?

I need to get the CPU Usage of a single process,

either by PID, or ProcessName..

Share this post


Link to post
Share on other sites
PsaltyDS

thank you PsaltyDS..

If it leaks, and I do need to use it repeatedly,

what else can I use?

I need to get the CPU Usage of a single process,

either by PID, or ProcessName..

Read the whole topic, there is a work-around posted where the refresher is created outside the loop.

:P


Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share this post


Link to post
Share on other sites
Zohar

ok

thank you!

do you know how do I access item #i from the collection there?

when I try $colItems.item(0), it gives an error..

Edited by Zohar

Share this post


Link to post
Share on other sites
PsaltyDS

ok

thank you!

do you know how do I access item #i from the collection there?

when I try $colItems.item(0), it gives an error..

According to Microsoft: Scripting in Windows Vista: What's New in WMI?

The .ItemIndex() property was added to WMI in Vista. It's not available before Vista (in WMI objects, anyway), and .index is not an alias for it.

If you are running Vista, then you might try $colItems.ItemIndex(0).

:P

Edited by PsaltyDS

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share this post


Link to post
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
Sign in to follow this  

×