Jump to content
Sign in to follow this  
pcguru000

CPU Usage

Recommended Posts

pcguru000

Im trying to make a little vertical progress bar that will show the current CPU Usage, this is what I have right now.

Global $wbemFlagReturnImmediately = 0x10
Global $wbemFlagForwardOnly = 0x20
Global $wbemFlags = $wbemFlagReturnImmediately + $wbemFlagForwardOnly
Global $strComputer = @ComputerName
$hGUI = GUICreate("CPUmon", 200, 200, -1, -1, -1)
$CPU = GUICtrlCreateProgress(130, 20, 64, 64,0x04)
GUISetState(@SW_SHOW, $hGUI)
$sMsg = ""
while 1
Global $objWMIService = ObjGet("winmgmts:\\" & $strComputer & "\root\CIMV2")
Global $colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_Processor", "WQL", $wbemFlags)
    For $objItem In $colItems
        $sMsg = $objItem.LoadPercentage     
    Next
    sleep(1000)
GUICtrlSetData($CPU, $sMsg)
wend

I really want to get a steady update (perhaps every half second)... What exactly is making this freeze each time it updates ( I loose control of the close button ect aswell)

Is doesnt seem to be the "sleep(1000)" - I think it something to do w/ this:

Global $objWMIService = ObjGet("winmgmts:\\" & $strComputer & "\root\CIMV2")
Global $colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_Processor", "WQL", $wbemFlags)

But why? How do I smooth this out?

Is there away I could just use the control that the default windows TaskManager uses...?

If so how?

THANKS!

Share this post


Link to post
Share on other sites
Monamo

Im trying to make a little vertical progress bar that will show the current CPU Usage, this is what I have right now.

Global $wbemFlagReturnImmediately = 0x10
Global $wbemFlagForwardOnly = 0x20
Global $wbemFlags = $wbemFlagReturnImmediately + $wbemFlagForwardOnly
Global $strComputer = @ComputerName
$hGUI = GUICreate("CPUmon", 200, 200, -1, -1, -1)
$CPU = GUICtrlCreateProgress(130, 20, 64, 64,0x04)
GUISetState(@SW_SHOW, $hGUI)
$sMsg = ""
while 1
Global $objWMIService = ObjGet("winmgmts:\\" & $strComputer & "\root\CIMV2")
Global $colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_Processor", "WQL", $wbemFlags)
    For $objItem In $colItems
        $sMsg = $objItem.LoadPercentage     
    Next
    sleep(1000)
GUICtrlSetData($CPU, $sMsg)
wend

I really want to get a steady update (perhaps every half second)... What exactly is making this freeze each time it updates ( I loose control of the close button ect aswell)

Is doesnt seem to be the "sleep(1000)" - I think it something to do w/ this:

Global $objWMIService = ObjGet("winmgmts:\\" & $strComputer & "\root\CIMV2")
Global $colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_Processor", "WQL", $wbemFlags)

But why? How do I smooth this out?

Is there away I could just use the control that the default windows TaskManager uses...?

If so how?

THANKS!

<!--QuoteEnd--></div><!--QuoteEEnd-->

The close button isn't working because you haven't assigned an event to handle it being clicked. As for the progress bar appearing to "freeze" on you, I saw the same thing with your progress bar dimensions. However, by making the progress bar considerably more narrow, I was seeing the status changes.

Mildly tested example:

AutoItSetOption("GuiOnEventMode",1)
Global $wbemFlagReturnImmediately = 0x10
Global $wbemFlagForwardOnly = 0x20
Global $wbemFlags = $wbemFlagReturnImmediately + $wbemFlagForwardOnly
Global $strComputer = @ComputerName

Global $objWMIService = ObjGet("winmgmts:\\" & $strComputer & "\root\CIMV2")

$hGUI = GUICreate("CPUmon", 200, 200, -1, -1, -1)
$CPU = GUICtrlCreateProgress(130, 20, 10, 120, 0x04)
$labelCPU = GUICtrlCreateLabel("",150,20,30,20,0x1000)
GUISetState(@SW_SHOW, $hGUI)

GUISetOnEvent(-3,"_ExitIt")
$sMsg = ""

While 1
    Global $colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_Processor", "WQL", $wbemFlags)

    For $objItem In $colItems
        $sMsg = $objItem.LoadPercentage
    Next
    GUICtrlSetData($CPU, $sMsg)
    GUICtrlSetData($labelCPU,$sMsg &"%")
    Sleep(10)
WEnd

Func _ExitIt()
    Exit
EndFunc
Edited by Monamo

- MoChr(77)& Chr(97)& Chr(100)& Chr(101)& Chr(32)& Chr(121)& Chr(97)& Chr(32)& Chr(108)& Chr(111)& Chr(111)& Chr(107)-------I've told you 100,000 times not to exaggerate!-------Don't make me hit you with my cigarette hand...-------My scripts:Random Episode Selector, Keyboard MouseMover, CopyPath v2.1, SmartRename for XP,Window Tracer[sup]New![/sup]

Share this post


Link to post
Share on other sites
pcguru000

Still locking my whole GUI... for instance take that script... throw a button on it and try clicking the button everything is locking up - almost as if the CPU was being over used but... its not .. its something in the loop I think but I cant seem to figure it...

Is there another way to get CPU usage?

PS

Thanks 4 the comment on the progress bar - that does help viewing it... too bad this still messing things up :/

Share this post


Link to post
Share on other sites
rasim

pcguru000

Welcome to the forum. Please in next time don't post your question in the PM and Example Scripts forum. ;)

Quick example:

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <ProgressConstants.au3>

Global $IDLETIME, $KERNELTIME, $USERTIME
Global $StartIdle, $StartKernel, $StartUser
Global $EndIdle, $EndKernel, $EndUser
Global $Timer

$IDLETIME   = DllStructCreate("dword;dword")
$KERNELTIME = DllStructCreate("dword;dword")
$USERTIME   = DllStructCreate("dword;dword")

$hGUI = GUICreate("CPUmon", 200, 150, -1, -1, -1, $WS_EX_TOPMOST)
GUISetIcon("shell32.dll", 13)

GUICtrlCreateLabel("Total CPU Usage:", 25, 20, 105, 20)
GUICtrlSetFont(-1, 8.5, 800, Default, "MS Sans Serif")

$ValueLabel = GUICtrlCreateLabel("", 130, 20, 40, 20)
GUICtrlSetFont(-1, 8.5, 800, Default, "MS Sans Serif")

$progress = GUICtrlCreateProgress(170, 20, 20, 120, BitOR($PBS_VERTICAL, $PBS_SMOOTH))
DllCall("UxTheme.dll", "int", "SetWindowTheme", "hwnd", GUICtrlGetHandle($progress), "wstr", "", "wstr", "")

GUISetState()

_TimerProc()

AdlibEnable("_TimerProc", 1000)

While 1
    $msg = GUIGetMsg()
    Switch $msg
        Case $GUI_EVENT_CLOSE
            ExitLoop
    EndSwitch
WEnd

Func _TimerProc()
    _GetSysTime($EndIdle, $EndKernel, $EndUser)
    _CPUCalc()
    _GetSysTime($StartIdle, $StartKernel, $StartUser)
EndFunc

Func _GetSysTime(ByRef $sIdle, ByRef $sKernel, ByRef $sUser)
    DllCall("kernel32.dll", "int", "GetSystemTimes", "ptr", DllStructGetPtr($IDLETIME), _
            "ptr", DllStructGetPtr($KERNELTIME), _
            "ptr", DllStructGetPtr($USERTIME))

    $sIdle = DllStructGetData($IDLETIME, 1)
    $sKernel = DllStructGetData($KERNELTIME, 1)
    $sUser = DllStructGetData($USERTIME, 1)
EndFunc   ;==>_GetSysTime

Func _CPUCalc()
    Local $iSystemTime, $iTotal, $iCalcIdle, $iCalcKernel, $iCalcUser
    
    $iCalcIdle   = ($EndIdle - $StartIdle)
    $iCalcKernel = ($EndKernel - $StartKernel)
    $iCalcUser   = ($EndUser - $StartUser)
    
    $iSystemTime = ($iCalcKernel + $iCalcUser)
    $iTotal = Int(($iSystemTime - $iCalcIdle) * (100 / $iSystemTime))
    
    If GUICtrlRead($ValueLabel) <> $iTotal & "%" Then
        ControlSetText($hGUI, "", $ValueLabel, $iTotal & "%")
        GUICtrlSetData($progress, $iTotal)
    EndIf
EndFunc   ;==>_CPUCalc

Share this post


Link to post
Share on other sites
pcguru000

Indeed I got your script working Rasim THANK YOU you are awesome.

I will post my questions elsewhere thanks again ;)

-brendon

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  

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.