Jump to content
Sign in to follow this  

Best way to have a - While 1 - loop

Recommended Posts


I have a GUI script that needs to:

1. run as tray item and preform a task once in X hours

2. respond to GUI clicks

3. respond to Tray clicks

what is the best way to do it ?

when I use the below code, It the script freezes from time to time so that GUI and Tray clicks are not run.

is there a different way to do so ?



current code

Global $iTimer = _NowCalc()

While 1

    $msg = TrayGetMsg()
    TrayItemSetState($msg, $TRAY_UNCHECKED)
                Case $msg = $updateitem

                Case $msg = $ShowSearchSlider
                Case $msg = $aboutitem
                    Msgbox(64, "About", "AboutMe.com")
                Case $msg = $exititem


;timer cheking - 1st tomer run once in 6 hours 

    If SecTimeDiff ($iTimer) >=  60  then  ;3 hours  = 10800 sec
    ; check if last run is > 6 hours  and func_do
    $iTimer = _NowCalc()
    $ini = IniRead("\Config.ini", "App" ,"LastChnage_1", "1900/01/01 00:00:00")
        If SecTimeDiff ($ini) >= 21600  then    
        $hdl = func_do_1 ()
;timer cheking - 2st once in 24 hours 

    $ini = IniRead("\Config.ini", "App" ,"LastChnage_2", "1900/01/01 00:00:00")
        If SecTimeDiff ($ini) >= 86400   then   
        $hdl = func_do_2 ()


; If mouse is on top of GUI do action Chng_GUI

    If  MouseGetPos(0) < $Hposition AND MouseGetPos(1) > $Vposition  Then 
    ElseIf  IsVisible($hwnd2) then 
        If ( MouseGetPos(0) > 250 OR MouseGetPos(1) < $Vposition - 50)  Then 

;sleep (10)


Func SecTimeDiff ($sTime)
    ; s seconds h hours m mintuts (??)
    $Diff = _DateDiff("s", $sTime, _NowCalc())
    return $Diff

Func IsVisible($handle)
    If BitAND(WinGetState($handle), 2) Then
        Return 1
        Return 0
EndFunc   ;==>IsVisible

Share this post

Link to post
Share on other sites


This is how I would do the waiting loops - look for the <<<<<<<<< lines: :P

#include <GUIConstantsEx.au3>
#Include <Date.au3>

Opt("TrayMenuMode", 3) ; This is the best way to prevent ticks appearing on the tray menu <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

$updateitem = TrayCreateItem("Update")
$ShowSearchSlider = TrayCreateItem("Show")
$aboutitem = TrayCreateItem("About")
$exititem = TrayCreateItem("Exit")

$hGUI = GUICreate("Test", 500, 500)

GUISetState(@SW_HIDE, $hGUI)

; Get DTG of last updates
$sUpdate_1 = IniRead("\Config.ini", "App", "LastChnage_1", "1900/01/01 00:00:00")
$sUpdate_2 = IniRead("\Config.ini", "App", "LastChnage_2", "1900/01/01 00:00:00")

; Start short term timer
Global $iBegin = TimerInit()

While 1

    Switch TrayGetMsg() ; I think Switch is clearer in a loop like this <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
        Case $updateitem
            MsgBox(0, "Running", "Update")
        Case $ShowSearchSlider
            MsgBox(0, "Running", "Set_Show")
            GUISetState(@SW_SHOW, $hGUI)
        Case $aboutitem
            MsgBox(64, "About", "AboutMe.com")
        Case $exititem

    Switch GUIGetMsg() ; We do need to check the GUI
        Case $GUI_EVENT_CLOSE
            GUISetState(@SW_HIDE, $hGUI) ; We hide it if we click the [X}

    ; Let us check the timers every minute
    If TimerDiff($iBegin) > 1000 Then ; For 1 minute use 60000 here <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

        ; Now check and see if we need to do something
        $iTimer = _NowCalc()
        ; We are looking to run here every 6 hours
        If  _DateDiff("s", $sUpdate_1, $iTimer) > 6 Then ; at least 6 hours have elapsed ; reset to "h" <<<<<<<<<<<<<<<<<<<<<<<<<
            ConsoleWrite("06" & " - " & @SEC & @CRLF) ; Delete <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
            ;MsgBox(0, "Running" , "func_do_1") ; Uncomment <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
            ; Reset $sUpdate_1
            $sUpdate_1 = $iTimer
            ; Here you need to rewrite the ini file
            ; IniWrite("\Config.ini", "App", "LastChnage_1", $iTimer) ; Uncomment <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
        ; We are looking to run here every 24 hours

        If  _DateDiff("s", $sUpdate_2, $iTimer) > 24 Then ; at least 24 hours have elapsed ; reset to "h" <<<<<<<<<<<<<<<<<<<<<<<<<
            ConsoleWrite("24" & " - " & @SEC & @CRLF) ; Delete <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
            ;MsgBox(0, "Running" , "func_do_2") ; Uncomment <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
            ; Reset $sUpdate_2
            $sUpdate_2 = $iTimer
            ; Here you need to rewrite the ini file and reset $sUpdate_2
            ; IniWrite("\Config.ini", "App", "LastChnage_2", $iTimer) ; Uncomment <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
        ;  Reset the short term timer to wait for another minute
        $iBegin = TimerInit()

    ; Mouse over GUI code


I have set it up to run every 6 and 24 seconds - the comments show where to change it back. At the moment it seems to run every 7/25 secs because it is running so quickly - if you use hours it should run to the accuracy to which you set the short term timer (I have suggested 1 minute).

I am unsure what you want to achieve with the mouse over GUI code so it is a bit hard to suggest anything. You obviously want different things to happen if the GUI is visible or not. What is the relationship between the GUI, $Hposition and $Vposition? :x


Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:


ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area


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  

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Create New...