Zohar Posted March 3, 2009 Share Posted March 3, 2009 (edited) 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 March 4, 2009 by Zohar Link to comment Share on other sites More sharing options...
PsaltyDS Posted March 3, 2009 Share Posted March 3, 2009 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. 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 Link to comment Share on other sites More sharing options...
Zohar Posted March 3, 2009 Author Share Posted March 3, 2009 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.. Link to comment Share on other sites More sharing options...
PsaltyDS Posted March 3, 2009 Share Posted March 3, 2009 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. 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 Link to comment Share on other sites More sharing options...
Zohar Posted March 4, 2009 Author Share Posted March 4, 2009 (edited) okthank 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 March 4, 2009 by Zohar Link to comment Share on other sites More sharing options...
PsaltyDS Posted March 4, 2009 Share Posted March 4, 2009 (edited) okthank 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). Edited March 4, 2009 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 Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now