Sign in to follow this  
Followers 0
nadigo

Best way to have a - While 1 - loop

2 posts in this topic

HI,

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 ?

Thanks

Nadav

current code

Global $iTimer = _NowCalc()


While 1

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

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

    EndSelect
    

;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 ()
        EndIf
    
    
;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 ()
        EndIf
    

EndIf 
    

; If mouse is on top of GUI do action Chng_GUI

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

;sleep (10)

WEnd



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

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

Share this post


Link to post
Share on other sites



nadigo,

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")
TrayCreateItem("")
$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
            Exit
    EndSwitch

    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}
    EndSwitch

    ; 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 <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
        EndIf
        ; 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 <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
        EndIf
        ;  Reset the short term timer to wait for another minute
        $iBegin = TimerInit()
    EndIf

    ; Mouse over GUI code

WEnd

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

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

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  
Followers 0