Jump to content

Break func loop by OnTrayEvent


Recommended Posts

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
Link to comment
Share on other sites

  • Moderators

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

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:

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

 

Link to comment
Share on other sites

  • Moderators

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

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:

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

 

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...