Sign in to follow this  
Followers 0
TheBrainiac

[SOLVED] Looping through WMI Query results very slow

3 posts in this topic

#1 ·  Posted (edited)

I'm using WMI to poll my system's CPU load:

$objWMIService = ObjGet("winmgmts:\\localhost\root\CIMV2")

$cpu = 0

While 1
    $init = TimerInit()
    $colItems = $objWMIService.ExecQuery("SELECT LoadPercentage FROM Win32_Processor", "WQL", 0x30)
    For $objItem In $colItems
        $cpu = $objItem.LoadPercentage
    Next
    ConsoleWrite($cpu & "%" & @CRLF)
    ConsoleWrite(TimerDiff($init) & @CRLF)
WEnd

 

 

The For-loop in this example suspiciously always takes exactly one second (1008-1012ms) to finish, while not consuming large amounts of CPU time. Using debug lines, I have also confirmed that $colItems contains just one element. In fact it is For $objItem In $colItems​ that's taking this one second.

I understand there will be some conversion from the WMI return value to actual usable data, but it looks very much like there is either a delay or a timeout of some sort going on. Has anyone had similar issues? Have you found a way to get rid of it? If not, is there a similarly easy way to get CPU load without WMI?

Thanks for your feedback.

Edited by TheBrainiac

Share this post


Link to post
Share on other sites



Ah, so it starts measuring CPU load and waits a second to get an average value. That explains the one second delay. Now this makes me wonder why it seemed to run fine in the past.

Thank you!

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  
Followers 0