Jump to content

Can't stop the loop via stop button.


Recommended Posts

Hi All,

I am a beginner on the area of AutoIT. I put two buttons upon my GUI. the two buttons are "Start" and "Stop".

When I press start button to run function. Meanwhile, I hope I can pause the function if I press "stop" button. But It seems not work.

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <GUIConstants.au3>
#include <GuiEdit.au3>
#include <GuiComboBox.au3>
#include <buttonconstants.au3>

HotKeySet("{ESC}", "alldone")   

$Form2          = Guicreate("test", 550, 550, -1, -1, BitOR($WS_MAXIMIZEBOX, $WS_MINIMIZEBOX, $WS_SIZEBOX, $WS_THICKFRAME, $WS_SYSMENU, $WS_CAPTION, $WS_OVERLAPPEDWINDOW, $WS_TILEDWINDOW, $WS_POPUP, $WS_POPUPWINDOW, $WS_GROUP, $WS_TABSTOP, $WS_BORDER, $WS_CLIPSIBLINGS))
$Edit1          = GUICtrlCreateEdit("", 190, 220, 340, 320, BitOR($ES_AUTOVSCROLL, $ES_AUTOHSCROLL, $ES_READONLY, $ES_WANTRETURN, $WS_HSCROLL, $WS_VSCROLL))
$Start          = GuiCtrlCreateButton("Start", 190, 80, 60, 40, $BS_ICON)
$Stop           = GuiCtrlCreateButton("Stop", 270, 80, 60, 40, $BS_ICON)
GUICtrlSetColor(-1, 0x008080)
GUISetState(@SW_SHOW)

Events()
GUICtrlSetState($Edit1, $GUI_FOCUS)


Func Events()
    Opt("GUIOnEventMode", 1)
    GUICtrlSetOnEvent($Start, "SendEvent")
    GUICtrlSetOnEvent($Stop, "stopscript")
EndFunc   

Func stopscript()
    while 1
        $msg = GUIGetMsg()
        If $msg <> $Start Then
            sleep (30000000)
        Else
            ExitLoop
        EndIf
    WEnd
EndFunc

Func SendEvent()    
    For $index = 1  To 50 Step 1
        
        Send("123" & @CR)
        Sleep(3000)
        Send("123" & @CR)
        Sleep(3000)
EndFunc
Edited by iorih0406
Link to comment
Share on other sites

  • Moderators

iorih0406,

Welcome to the AutoIt forum. :idea:

Unfortunately you cannot break into a running function as AutoIt queues the calls so that the "stopscript" will only ever be called once the "SendEvent" function has terminated.

There are 2 ways around this if (like in this case) there are suitable pauses in the function:

- 1. You can use MessageLoop mode and look for the Stop button while you are paused:

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <GUIConstants.au3>
#include <GuiEdit.au3>
#include <GuiComboBox.au3>
#include <buttonconstants.au3>

;HotKeySet("{ESC}", "alldone")

$Form2 = GUICreate("test", 550, 550, -1, -1, BitOR($WS_MAXIMIZEBOX, $WS_MINIMIZEBOX, $WS_SIZEBOX, $WS_THICKFRAME, $WS_SYSMENU, $WS_CAPTION, $WS_OVERLAPPEDWINDOW, $WS_TILEDWINDOW, $WS_POPUP, $WS_POPUPWINDOW, $WS_GROUP, $WS_TABSTOP, $WS_BORDER, $WS_CLIPSIBLINGS))
$Edit1 = GUICtrlCreateEdit("", 190, 220, 340, 320, BitOR($ES_AUTOVSCROLL, $ES_AUTOHSCROLL, $ES_READONLY, $ES_WANTRETURN, $WS_HSCROLL, $WS_VSCROLL))
$Start = GUICtrlCreateButton("Start", 190, 80, 60, 40, $BS_ICON)
$Stop = GUICtrlCreateButton("Stop", 270, 80, 60, 40, $BS_ICON)
GUICtrlSetColor(-1, 0x008080)
GUISetState(@SW_SHOW)

GUICtrlSetState($Edit1, $GUI_FOCUS)

While 1

    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Start
            SendEvent()
    EndSwitch

WEnd

Func SendEvent()

    ConsoleWrite("Starting" & @CRLF)

    For $index = 1 To 50 Step 1

        ConsoleWrite("Send 1" & @CRLF)
        ;Send("123" & @CR)
        $iBegin = TimerInit()
        While TimerDiff($iBegin) < 3000
            If GUIGetMsg() = $Stop Then
                ConsoleWrite("Stopping" & @CRLF)
                Return
            EndIf
            Sleep(10)
        WEnd
        ;Sleep(3000)
        
        ConsoleWrite("Send 2" & @CRLF)
        ;Send("123" & @CR)
        $iBegin = TimerInit()
        While TimerDiff($iBegin) < 3000
            If GUIGetMsg() = $Stop Then
                ConsoleWrite("Stopping" & @CRLF)
                Return
            EndIf
            Sleep(10)
        WEnd
        ;Sleep(3000)

    Next

EndFunc   ;==>SendEvent

- 2. You can keep OnEvent mode and use a HotKey to set a flag - here I have used F1. This works because HotKeys do break into running functions - but the function restarts once the HotKey function terminates :

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <GUIConstants.au3>
#include <GuiEdit.au3>
#include <GuiComboBox.au3>
#include <buttonconstants.au3>

Global $fStop = False

HotKeySet("{ESC}", "alldone")
HotKeySet("{F1}", "_stopscript")

$Form2 = GUICreate("test", 550, 550, -1, -1, BitOR($WS_MAXIMIZEBOX, $WS_MINIMIZEBOX, $WS_SIZEBOX, $WS_THICKFRAME, $WS_SYSMENU, $WS_CAPTION, $WS_OVERLAPPEDWINDOW, $WS_TILEDWINDOW, $WS_POPUP, $WS_POPUPWINDOW, $WS_GROUP, $WS_TABSTOP, $WS_BORDER, $WS_CLIPSIBLINGS))
GUISetOnEvent($GUI_EVENT_CLOSE, "alldone")
$Edit1 = GUICtrlCreateEdit("", 190, 220, 340, 320, BitOR($ES_AUTOVSCROLL, $ES_AUTOHSCROLL, $ES_READONLY, $ES_WANTRETURN, $WS_HSCROLL, $WS_VSCROLL))
$Start = GUICtrlCreateButton("Start", 190, 80, 60, 40, $BS_ICON)
;$Stop = GUICtrlCreateButton("Stop", 270, 80, 60, 40, $BS_ICON)
;GUICtrlSetColor(-1, 0x008080)
GUISetState(@SW_SHOW)

Events()
GUICtrlSetState($Edit1, $GUI_FOCUS)

While 1
    Sleep(10)
WEnd

Func Events()
    Opt("GUIOnEventMode", 1)
    GUICtrlSetOnEvent($Start, "SendEvent")
EndFunc

Func SendEvent()

    ConsoleWrite("Starting" & @CRLF)

    For $index = 1 To 50 Step 1

        ConsoleWrite("Send 1" & @CRLF)
        ;Send("123" & @CR)
        $iBegin = TimerInit()
        While TimerDiff($iBegin) < 3000
            If $fStop = True Then
                ConsoleWrite("Stopping" & @CRLF)
                $fStop = False
                Return
            EndIf
            Sleep(10)
        WEnd
        ;Sleep(3000)

        ConsoleWrite("Send 2" & @CRLF)
        ;Send("123" & @CR)
        $iBegin = TimerInit()
        While TimerDiff($iBegin) < 3000
            If $fStop = True Then
                ConsoleWrite("Stopping" & @CRLF)
                $fStop = False
                Return
            EndIf
            Sleep(10)
        WEnd
        ;Sleep(3000)

    Next

EndFunc   ;==>SendEvent

Func _stopscript()

    $fStop = True

EndFunc

Func alldone()
    Exit
EndFunc

You need to look at the syntax of your Send function by the way - you do not Send @CR like that! :(

I hope that is 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

you do not Send @CR like that! :idea:

Hey M23,

What's wrong with that syntax?

Do you mean as opposed to @CRLF?

I often use @CR when I want to send only @CR, or am I missing something?

edit: Ugh brackets...

Edited by hawky358
Link to comment
Share on other sites

Yea @CR = Chr(13) which is an acceptable send()

In any event ok I get what you're saying, in this situation iorih probably wanted to send an ENTER.

I sometimes just use @CR to signal the end of a command over TCP or doing a send to a certain window.

Edited by hawky358
Link to comment
Share on other sites

This works because HotKeys do break into running functions - but the function restarts once the HotKey function terminates :

I was curious because I didn't know this part about the function restarting. I wrote a simple test and it does not seem to restart.

HotKeySet("{ESC}","Pause")
Func Pause()
    Sleep(3000)
EndFunc
Func Loop()
    For $i = 100000 To 0 Step -1
        ToolTip($i)
    Next
EndFunc
Loop()

The loop runs and begins counting, Esc pauses, 3 seconds later the counting resumes right where it left off. No restarting.

Link to comment
Share on other sites

  • Moderators

ShawnW,

Sorry if I confused you. hawky358 is correct.

To make it absolutely clear:

A HotKey will (in nearly all cases*) interrupt a running function. Once the HotKey function ends, the original function resumes at the point it was halted.

I hope that resolves the matter. :idea:

M23

* Read the Help file to see when it does not. :)

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