Sign in to follow this  
Followers 0
VVvKamper

Break func loop by OnTrayEvent

4 posts in this topic

I'm trying to interrupt the running function as well as in http://www.autoitscript.com/wiki/Interrupting_a_running_function but using TrayItemSetOnEvent instead of GUICtrlSetOnEvent. The problem is that this code does not work and i cant figure out why. 

#NoTrayIcon

Opt("TrayMenuMode", 1)
Opt("TrayOnEventMode", 1)
TrayCreateItem("Start")
TrayItemSetOnEvent(-1, "Start")
TrayCreateItem("Stop")
TrayItemSetOnEvent(-1, "Stop")
Global $f=1

TraySetState()
While 1
   Sleep (50)
WEnd

Func Start()
   While 1
      If $f = 0 Then 
         ExitLoop
      EndIf
      MsgBox(64,"f", $f)
   WEnd
   MsgBox(64, "hi", "stop")
   Exit
EndFunc

Func Stop()
   $f = 0
EndFunc

Share this post


Link to post
Share on other sites



VVvKamper,

Here is one way to do it:

#include <GUIConstantsEx.au3>

Opt("TrayMenuMode", 1)
Opt("TrayOnEventMode", 1)
$cStart = TrayCreateItem("Start")
TrayItemSetOnEvent(-1, "Start")
$cStop = TrayCreateItem("Stop")
TrayItemSetOnEvent(-1, "Stop")
TrayItemSetState($cStop, $GUI_DISABLE)

; Not running at first
Global $f = 0

TraySetState()
While 1
    Sleep(10)
    ; Look for start
    If $f = 1 Then
        ; Adjust tray menu items
        TrayItemSetState($cStart, $GUI_DISABLE)
        TrayItemSetState($cStop, $GUI_ENABLE)
        ; Run the function
        _Func()
    EndIf
WEnd

Func _Func()

    $nBegin = TimerInit()
    ConsoleWrite("Running" & @CRLF)
    While 1
        ; Look for the flag
        If $f = 0 Then
            Exit
        EndIf
        ; Show function running
        If TimerDiff($nBegin) > 5000 Then
            ConsoleWrite("Running" & @CRLF)
            $nBegin = TimerInit()
        EndIf
    WEnd

EndFunc   ;==>_Func

Func Start()
    $f = 1
EndFunc   ;==>Start

Func Stop()
    $f = 0
EndFunc   ;==>Stop
All clear? Please ask if not. :)

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

I do not understand why events a not called in my example when i in start() loop, but i understand your solution and it helps me, thank you)

Share this post


Link to post
Share on other sites

VVvKamper,

As explained in the tutorial, AutoIt will not let you break into running functions easily. So when you entered your Start function via an OnEvent call you could never action the Stop to end it. That is why you need to enter the function from the idle loop as I showed. :)

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