Jump to content

[SOLVED] Looping through WMI Query results very slow

Recommended Posts

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
    ConsoleWrite($cpu & "%" & @CRLF)
    ConsoleWrite(TimerDiff($init) & @CRLF)



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
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
  • Recently Browsing   0 members

    No registered users viewing this page.

  • Create New...