Jump to content
Sign in to follow this  
eagle4life69

How to make my code not Suck up memory?

Recommended Posts

eagle4life69

Here is my code. What I have done is check to see if the Window Acrobat is open if it is grab the file name and log it. My co-workers do not like to clock in on jobs and this is how I make sure they do. Problem is that I am sucking the life out of memory and do not want it to lag the system.

Can someone please help me optimize the code so it can still check if the window is open and log the job even if it is only open for less than a minute.

Here is the basic part of my code "With secure info removed"

If _Singleton("Tracker2", 1) = 0 Then Exit
#include "Misc.au3"
#include <GUIConstants.au3>
Opt("TrayIconHide",1)
HotKeySet("!+q","Quit")
$ini=@ScriptDir & "\tracker2.ini"
Local $hQuery, $aRow, $sMsg, $1Query, $bRow, $aResult, $iRows, $iColumns, $iRval
AutoItSetOption("WinDetectHiddenText", 1)
AutoItSetOption("WinSearchChildren",1)
While 1
$var = WinList()
For $i = 1 to $var[0][0]
$clear=1
$x=0
If $var[$i][0] <> "" AND IsVisible($var[$i][1]) Then
  If StringInStr($var[$i][0],"Adobe Acrobat") Then
   $a=StringSplit($var[$i][0]," - ",1)
   $a=StringLeft($a[1],6)
   $a=StringSplit($a,".")
   $job=StringStripWS($a[1],2)
   If $job="Adobe" Then
   Else
    $employee=@ComputerName
    $hour=(60*@HOUR)+@MIN
    $info = IniReadSection($ini,$job)
    If @error=1 Then
     $info=$hour-90
    Else
     $info=$info[1][1]
    EndIf
    $subtract=($hour-$info)
    If $subtract > '60' Then
     If $job>'1000' Then
;~    MsgBox(0,$subtract,$job)
      $now = @MON & "-" & @MDAY & "-" & @YEAR  & " " & @HOUR & ":" & @MIN & ":" & @SEC
      $location=@ComputerName
      ;this is the section where I write to a secure site
      IniWrite($ini,$job,"Time",$hour)
      $x=1
     EndIf
    EndIf
   EndIf
  EndIf
EndIf
Next
If Not ProcessExists("Acrobat.exe") Then
FileDelete($ini)
EndIf
Sleep(500)
WEnd
Func Quit()
ProcessClose("relaunch.exe")
Exit
EndFunc
Func IsVisible($handle)
  If BitAnd( WinGetState($handle), 2 ) Then
    Return 1
  Else
    Return 0
  EndIf
EndFunc

Share this post


Link to post
Share on other sites
James

You're running a For... Next loop every 500 milliseconds - that's quite a lot of looping you have going on.

I'd offload the Window checking into AdlibRegister and create an event based loop which will sleep until the Adlib callback finds the window you're looking for.

Share this post


Link to post
Share on other sites
eagle4life69

without rewriting my code can you give me an example on how I would properly write the AdlibRegister call? the help file is not detailed enough. would i write it so the act of looking is in that call? Let me know thanks

Share this post


Link to post
Share on other sites
qsek

I dont see why this script would have any memory build up. Certainly not when the window is not open. Unless you have any global variable to add stuff into in your final version of your script, i dont see why it should either when the window is open.

However i did notice that the process takes about 2% CPU time on a single 4GHz.

Could be because you use winlist/for next Loop/StringinStr/isVisible just to check all your windows when you only need to use one command: WinActive

If _Singleton("Tracker2", 1) = 0 Then Exit
#include "Misc.au3"
#include <GUIConstants.au3>
Opt("TrayIconHide", 1)
HotKeySet("!+q", "Quit")
$ini = @ScriptDir & "tracker2.ini"
Local $hQuery, $aRow, $sMsg, $1Query, $bRow, $aResult, $iRows, $iColumns, $iRval
AutoItSetOption("WinDetectHiddenText", 1)
AutoItSetOption("WinSearchChildren", 1)
Opt("WinTitleMatchMode",2)

While 1
    Sleep(500)
    $ADhwnd = WinActive("Adobe Acrobat")
    If $ADhwnd Then
        ConsoleWrite("found" & @CRLF)
        $ADTitle = WinGetTitle($ADhwnd)
        $a = StringSplit($ADTitle, " - ", 1)
        $a = StringLeft($a[1], 6)
        $a = StringSplit($a, ".")
        $job = StringStripWS($a[1], 2)
        ConsoleWrite($job & @CRLF)
        If Not ($job = "Adobe") Then
            $employee = @ComputerName
            $hour = (60 * @HOUR) + @MIN
            $info = IniReadSection($ini, $job)
            If @error = 1 Then
                $info = $hour - 90
            Else
                $info = $info[1][1]
            EndIf
            $subtract = ($hour - $info)
            If $subtract > '60' Then
                If $job > '1000' Then
                    $now = @MON & "-" & @MDAY & "-" & @YEAR & " " & @HOUR & ":" & @MIN & ":" & @SEC
                    $location = @ComputerName
                    ;this is the section where I write to a secure site
                    IniWrite($ini, $job, "Time", $hour)
                EndIf
            EndIf
        EndIf
        Sleep(5000); To prevent writing to the ini every 500ms (if window exists)
    EndIf

    If Not ProcessExists("Acrobat.exe") Then
        FileDelete($ini)
    EndIf
    Sleep(500)
WEnd
Func Quit()
    ProcessClose("relaunch.exe")
    Exit
EndFunc   ;==>Quit
Func IsVisible($handle)
    If BitAND(WinGetState($handle), 2) Then
        Return 1
    Else
        Return 0
    EndIf
EndFunc   ;==>IsVisible

I dont understand your way of logging the time to do any optimization on this part. Script comments would help.

Also stripped $x and $clear since not used for anything.

Edited by qsek

Teamspeak 3 User Viewer - Quick and functional TS3 Query script, which shows online users.Cached Screenshot Deleter - Deletes older Fraps Screenshots if they exceed a specified limit.Unresolved Topics:Intercept and modify dragdrop text behaviour in scite

Share this post


Link to post
Share on other sites
BrewManNH

Wouldn't something like this be more efficient?

If _Singleton("Tracker2", 1) = 0 Then Exit
#include "Misc.au3"
#include <GUIConstants.au3>
Opt("TrayIconHide", 1)
HotKeySet("!+q", "Quit")
$ini = @ScriptDir & "tracker2.ini"
Local $hQuery, $aRow, $sMsg, $1Query, $bRow, $aResult, $iRows, $iColumns, $iRval
AutoItSetOption("WinDetectHiddenText", 1)
AutoItSetOption("WinSearchChildren", 1)
While 1
    $var = WinList("Adobe Acrobat")
    If $var[0][0] > 0 Then
        $clear = 1
        $x = 0
        $job = "Adobe"
        For $I = 1 To $var[0][0]
            If $var[$I][0] <> "" And IsVisible($var[$I][1]) Then
                $employee = @ComputerName
                $hour = (60 * @HOUR) + @MIN
                $info = IniReadSection($ini, $job)
                If @error = 1 Then
                    $info = $hour - 90
                Else
                    $info = $info[1][1]
                EndIf
                $subtract = ($hour - $info)
                If $subtract > '60' Then
                    If $job > '1000' Then
;~    MsgBox(0,$subtract,$job)
                        $now = @MON & "-" & @MDAY & "-" & @YEAR & " " & @HOUR & ":" & @MIN & ":" & @SEC
                        $location = @ComputerName
                        ;this is the section where I write to a secure site
                        IniWrite($ini, $job, "Time", $hour)
                        $x = 1
                    EndIf
                EndIf
            EndIf
        Next
    EndIf
    If Not ProcessExists("Acrobat.exe") Then
        FileDelete($ini)
    EndIf
    Sleep(500)
WEnd
Func Quit()
    ProcessClose("relaunch.exe")
    Exit
EndFunc   ;==>Quit
Func IsVisible($handle)
    If BitAND(WinGetState($handle), 2) Then
        Return 1
    Else
        Return 0
    EndIf
EndFunc   ;==>IsVisible
I changed your WinList so that it only looks for the window you're interested in, Adobe Acrobat, instead of listing ALL the windows and looking to see if it's there. If it doesn't find the window at all (the first If/Then statement), then bypass all of the rest of the code. Also, your code deletes your INI file as soon as Acrobat is closed, how is it supposed to read the values from it in this script if you keep deleting it?

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites
qsek

@BrewManNH

I think he use some kind of mysql query to write the time to a database in his real script.

That ini stuff is just an example for writing the data.


Teamspeak 3 User Viewer - Quick and functional TS3 Query script, which shows online users.Cached Screenshot Deleter - Deletes older Fraps Screenshots if they exceed a specified limit.Unresolved Topics:Intercept and modify dragdrop text behaviour in scite

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  

×