Jump to content

EEEK! It hogs the CPU!


Recommended Posts

So we use a tool here that monitors phone statistics, but in short.... it sucks.

I want to build a client side tool that monitors the state this app is in and times how long each state is engaged.

I submitted another post about how to search memory, and I think I have that all worked out ... somewhat.

Previous Help Post

So HUGE Thank YOU to all those who posted on that thread. :)

I now have a script put together that seems to be working ok except that it's hogging CPU, and I was wondering if anybody here might know why or how I could trim the CPU usage a bit.

Here's the actual script:

; 
#Include <WinAPI.au3>
#include <GUIConstants.au3>
#include <Date.au3>
#include <Memory.au3>
#include <File.au3> ; logfile
opt("TrayIconDebug",1)
Global $Secs, $Mins, $Hour, $Time
Global $globalACD, $globalAVAIL, $globalSTAFF, $globalAUX, $globalACW, $auxTime=0, $acwTime=0, $callTime=0, $inTime=0, $doOnce=0

$livestatsgui= GUICreate("LiveStats", 144, 312, 552, 183)
$grpCT = GUICtrlCreateGroup("Call Time", 2, 32, 142, 100)
$lblCcall = GUICtrlCreateLabel("In Queue", 75, 45, 60, 17)
$lblCcall = GUICtrlCreateLabel("Current Call", 10, 45, 60, 17)
$lblTcall = GUICtrlCreateLabel("Total Call/AutoIn", 25, 85, 90, 17)
GUICtrlCreateGroup("", -99, -99, 1, 1)
$grpAT = GUICtrlCreateGroup("Aux Time", 2, 152, 142, 160)
$lblCaux = GUICtrlCreateLabel("Current Aux", 10, 170, 75, 17)
$lblTaux = GUICtrlCreateLabel("Total Aux", 10, 205, 75, 17)
$lblTaux = GUICtrlCreateLabel("Current ACW", 75, 170, 75, 17)
$lblTaux = GUICtrlCreateLabel("Total ACW", 75, 206, 75, 17)
$lblTcall = GUICtrlCreateLabel("Total AUX/ACW", 25, 250, 90, 17)
GUICtrlCreateGroup("", -99, -99, 1, 1)
GUISetState(@SW_SHOW)

$inqueue = GUICtrlCreateLabel("00:00:00", 10, 60)
$incall = GUICtrlCreateLabel("00:00:00", 75, 60)
$TotalCallTime = GUICtrlCreateLabel("00:00:00", 45, 100)
$AuxTimer = GUICtrlCreateLabel("00:00:00", 10, 186)
$TotalTimeAux = GUICtrlCreateLabel("00:00:00", 10, 220)
$AcwTimer = GUICtrlCreateLabel("00:00:00", 75, 186)
$TotalTimeACW = GUICtrlCreateLabel("00:00:00", 75, 220)
$TotalTimeAUXACW = GUICtrlCreateLabel("00:00:00", 45, 270)
;~          $btnEndCall = GUICtrlCreateButton("Aux Out", 8, 255, 113, 33, 0)
;~          GUICtrlSetState($btnEndCall, $GUI_HIDE)
;~          $btnAnswer = GUICtrlCreateButton("Auto-In", 8, 255, 113, 33, 0)
;~ $btnCap = GUICtrlCreateButton("", 130, 65)
;~          GUICtrlCreateLabel("Current Mode: Aux'd Out", 5, 5, 150, 17)
;~          $SoT = GUICtrlCreateCheckbox("", 5, 295,15, 15)
;~          GUICtrlCreateLabel("Stay on top", 25, 295)
GUISetState()
AdlibEnable("Timer", 800)
;

If Not ProcessExists("qui.exe") Then Exit ; exit if calculator is not running
Dim $procHwnd = _WinAPI_OpenProcess($PROCESS_ALL_ACCESS, False, ProcessExists("qui.exe"))
If Not $procHwnd Then _Exit("Error while getting process handle!")

While 1
 ;FileWriteLine("debug.log",@min & ":" & @sec & " ==> before")
  $msg = GUIGetMsg()
 ;FileWriteLine("debug.log",@min & ":" & @sec & " ==> after")
  Select
    Case $msg = $GUI_EVENT_CLOSE
        IniWrite ( @ScriptDir&"\LiveStats.ini", "Globals", "$globalACD", $globalACD )
        IniWrite ( @ScriptDir&"\LiveStats.ini", "Globals", "$globalAVAIL", $globalAVAIL )
        IniWrite ( @ScriptDir&"\LiveStats.ini", "Globals", "$globalSTAFF", $globalSTAFF )
        IniWrite ( @ScriptDir&"\LiveStats.ini", "Globals", "$globalAUX", $globalAUX )
        IniWrite ( @ScriptDir&"\LiveStats.ini", "Globals", "$globalACW", $globalACW )
        Exit
EndSelect
    if $doOnce=0 Then
        $inidate = IniRead ( @ScriptDir&"\LiveStats.ini", "Date Reminder", "Today's Date", "" )
        $todaysdate = _NowDate()
        if $inidate = $todaysdate Then
                $globalACD= IniRead ( @ScriptDir&"\LiveStats.ini", "Globals", "$globalACD", $globalACD )
                $globalAVAIL= IniRead ( @ScriptDir&"\LiveStats.ini", "Globals", "$globalAVAIL", $globalAVAIL )
                $globalSTAFF= IniRead ( @ScriptDir&"\LiveStats.ini", "Globals", "$globalSTAFF", $globalSTAFF )
                $globalAUX= IniRead ( @ScriptDir&"\LiveStats.ini", "Globals", "$globalAUX", $globalAUX )
                $globalACW= IniRead ( @ScriptDir&"\LiveStats.ini", "Globals", "$globalACW", $globalACW )
                $doOnce=13
            EndIf
        EndIf
Wend

;  IniWrite ( @ScriptDir&"\imagelog.ini", "Errors", "mcupdate", "I did not find mcupdate.exe in default directories")
;~      RESULT 1 numbers
;~      8900000099 - auto'ed in
;~      8A00000098 - auxed 
;~      8B0000009A - ACW ****not used?
;~      8B0000009C - ACW
;~      Result 2 Numbers 0x01F1915F
;~      005000000 = After Call Work
;~      001000000 = Everything Else
        
Func Timer()
    Dim $pBuffer = DllStructCreate("byte[256]"), $iRead = 0  ;04B11DB8
    _WinAPI_ReadProcessMemory($procHwnd, 0x04B11DB8, DllStructGetPtr($pBuffer), 5, $iRead)
    If Not $iRead Then _Exit("Error while reading data!")
    $result= strclean($pBuffer)
;       MsgBox(0, "Data Read", $result)
    _WinAPI_ReadProcessMemory($procHwnd, 0x01F1915F, DllStructGetPtr($pBuffer), 5, $iRead)
    If Not $iRead Then _Exit("Error while reading data!")
    $result2= strclean($pBuffer)
;           Global $globalACD, $globalAVAIL, $globalSTAFF, $globalAUX, $globalACW   
;   _DebugPrint("$Result: "&$result)
;   _DebugPrint("$Result2: "&$result2)
    Switch $result
        Case "8900000099" ;- auto'ed in
                if($inTime=0) then
                $inTime=TimerInit()
            EndIf
                    _TicksToTime(Int(TimerDiff($inTime)), $Hour, $Mins, $Secs )
                    $test=StringFormat("%02i:%02i:%02i", $Hour, $Mins, $Secs)
                    If GUICtrlRead ( $incall ) <> $test Then  GUICtrlSetData ( $incall, $test )         
                if not ($auxTime=0) then
                $globalAUX += TimerDiff($auxTime)
                $auxTime=0
            EndIf
                if not ($acwTime=0) then
                $globalACW += TimerDiff($acwTime)
                $acwTime=0
            EndIf
                if not ($callTime=0) then
                $globalACD += TimerDiff($callTime)
                $callTime=0
            EndIf
        Case "8A00000098"  ;- auxed 
                if($auxTime=0) then
                $auxTime=TimerInit()
            EndIf
                    _TicksToTime(Int(TimerDiff($auxTime)), $Hour, $Mins, $Secs )
                    $test=StringFormat("%02i:%02i:%02i", $Hour, $Mins, $Secs)
                    If GUICtrlRead ( $AuxTimer ) <> $test Then  GUICtrlSetData ( $AuxTimer, $test )
                if not ($acwTime=0) then
                $globalACW += TimerDiff($acwTime)
                $acwTime=0
                EndIf
                if not ($callTime=0) then               
                $globalACD += TimerDiff($callTime)
                $callTime=0
                EndIf
                if not ($inTime=0) then             
                $globalAVAIL += TimerDiff($inTime)
                $inTime=0
                EndIf
;       Case "8B0000009A" Or "8B0000009C";-ACW OR in call
        Case "8B0000009C" ;-ACW OR in call
            if $result2 = "0005000000" Then  ;-ACW
                if($acwTime=0) then
                $acwTime=TimerInit()
            EndIf
                    _TicksToTime(Int(TimerDiff($acwTime)), $Hour, $Mins, $Secs )
                    $test=StringFormat("%02i:%02i:%02i", $Hour, $Mins, $Secs)
                    If GUICtrlRead ( $AcwTimer ) <> $test Then  GUICtrlSetData ( $AcwTimer, $test )
                    if not ($auxTime=0) then            
                    $globalAUX += TimerDiff($auxTime)
                    $auxTime=0
                    EndIf
                    if not ($inTime=0) then                 
                    $globalAVAIL += TimerDiff($inTime)
                    $inTime=0
                    EndIf
                    if not ($callTime=0) then           
                    $globalACD += TimerDiff($callTime)
                    $callTime=0
                    EndIf
            Else
                if($callTime=0) then
                $callTime=TimerInit()   ;- IN CALL
                EndIf
                    _TicksToTime(Int(TimerDiff($callTime)), $Hour, $Mins, $Secs )
                    $test=StringFormat("%02i:%02i:%02i", $Hour, $Mins, $Secs)
                    If GUICtrlRead ( $incall ) <> $test Then  GUICtrlSetData ( $incall, $test )
                    if not ($auxTime=0) then
                    $globalAUX += TimerDiff($auxTime)
                    $auxTime=0
                    EndIf
                    if not ($inTime=0) then     
                    $globalAVAIL += TimerDiff($inTime)
                    $inTime=0
                    EndIf
                    if not ($acwTime=0) then                
                    $globalACW += TimerDiff($acwTime)
                    $acwTime=0
                EndIf
            EndIf
    EndSwitch
;~          Global $globalACD, $globalAVAIL, $globalSTAFF, $globalAUX, $globalACW   
$inidate = IniRead ( @ScriptDir&"\LiveStats.ini", "Date Reminder", "Today's Date", "" )
$todaysdate = _NowDate()
;_DebugPrint("$todaysdate: "&$todaysdate)
if not $inidate = $todaysdate Then
IniWrite ( @ScriptDir&"\LiveStats.ini", "Date Reminder", "Today's Date", $todaysdate )
_FileWriteLog ( @ScriptDir&"\LiveStats.log", "Today's Date"&$todaysdate )
_FileWriteLog ( @ScriptDir&"\LiveStats.log", "$globalACD"&$globalACD )
_FileWriteLog ( @ScriptDir&"\LiveStats.log", "$globalAVAIL"&$globalAVAIL )
_FileWriteLog ( @ScriptDir&"\LiveStats.log", "$globalSTAFF"&$globalSTAFF )
_FileWriteLog ( @ScriptDir&"\LiveStats.log", "$globalAUX"&$globalAUX )
_FileWriteLog ( @ScriptDir&"\LiveStats.log", "$globalACW"&$globalACW )
EndIf
IniWrite ( @ScriptDir&"\LiveStats.ini", "Globals", "$globalACD", $globalACD )
IniWrite ( @ScriptDir&"\LiveStats.ini", "Globals", "$globalAVAIL", $globalAVAIL )
IniWrite ( @ScriptDir&"\LiveStats.ini", "Globals", "$globalSTAFF", $globalSTAFF )
IniWrite ( @ScriptDir&"\LiveStats.ini", "Globals", "$globalAUX", $globalAUX )
IniWrite ( @ScriptDir&"\LiveStats.ini", "Globals", "$globalACW", $globalACW )

        _TicksToTime(Int($globalAUX), $Hour, $Mins, $Secs )
        $test=StringFormat("%02i:%02i:%02i", $Hour, $Mins, $Secs)
    If GUICtrlRead ( $TotalTimeAux ) <> $test Then  GUICtrlSetData ( $TotalTimeAux, $test )
    
            _TicksToTime(Int($globalACW), $Hour, $Mins, $Secs )
        $test=StringFormat("%02i:%02i:%02i", $Hour, $Mins, $Secs)
    If GUICtrlRead ( $TotalTimeACW ) <> $test Then  GUICtrlSetData ( $TotalTimeACW, $test )
    
            _TicksToTime(Int($globalACD+$globalAVAIL), $Hour, $Mins, $Secs )
        $test=StringFormat("%02i:%02i:%02i", $Hour, $Mins, $Secs)
    If GUICtrlRead ( $TotalCallTime ) <> $test Then  GUICtrlSetData ( $TotalCallTime, $test )
    
            _TicksToTime(Int($globalACW+$globalAUX), $Hour, $Mins, $Secs )
        $test=StringFormat("%02i:%02i:%02i", $Hour, $Mins, $Secs)
    If GUICtrlRead ( $TotalTimeAUXACW ) <> $test Then  GUICtrlSetData ( $TotalTimeAUXACW, $test )


EndFunc  ;==>Timer

Func _Exit($s_Msg)
   
    MsgBox(0, "Error", $s_Msg)
    Exit
   
EndFunc

Func strclean($pBuffer)
    $rawstr= Binary(DllStructGetData($pBuffer, 1))
 $rawstrlen= StringLen($rawstr)
 $trimamount= $rawstrlen - 12
 $trimmedstr= StringTrimRight ( $rawstr, $trimamount )
 $trimamount= 2
 $trimmedstr= StringTrimLeft ( $trimmedstr, $trimamount )
return $trimmedstr
EndFunc

Func _DebugPrint($s_text)
    $s_text = StringReplace($s_text, @LF, @LF & "-->")
    ConsoleWrite("!===========================================================" & @LF & _
            "+===========================================================" & @LF & _
            "-->" & $s_text & @LF & _
            "+===========================================================" & @LF)
EndFunc   ;==>_DebugPrint

Now I think the heavy CPU usage may be coming from the calls to _WinAPI_ReadProcessMemory, but I'm not sure how to trim that down if thats the case. I changed AdlibEnable("Timer", 800) to 800 from 50 and that helped dramatically, but I don't think it should update less than that.

Edited by bobsyuruncle
Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...