Jump to content

How to stop-interrupt a loop-function with a stop button


TAMIRI
 Share

Recommended Posts

Hello,

i was wondering how could i stop a loop with a button

here is my code

#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#Region ### START Koda GUI section ### Form=
Global $Form1 = GUICreate("Form1", 623, 444, 192, 114)
Global $StartButton = GUICtrlCreateButton("Button1", 88, 192, 171, 25)
Global $EndButton = GUICtrlCreateButton("Button2", 320, 192, 203, 25)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

Global $var = 1

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit

        Case $StartButton
            Start()
        Case $EndButton
            Stop()
    EndSwitch
WEnd


Func Start()
    While $var = 1
        ConsoleWrite("again" & @CRLF)
    WEnd
EndFunc

Func Stop()
    $var = 0
EndFunc

i was reading https://www.autoitscript.com/wiki/Interrupting_a_running_function

but still i could not figure it out

help PLZ !

Link to comment
Share on other sites

  • Moderators

TAMIRI,

Not too difficult:

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

Global $bInterrupt = False

Global $Form1 = GUICreate("Form1", 623, 444, 192, 114)
Global $StartButton = GUICtrlCreateButton("Start", 88, 192, 171, 25)
Global $EndButton = GUICtrlCreateButton("Stop", 320, 192, 203, 25)
GUICtrlSetState($EndButton, $GUI_DISABLE)

GUISetState(@SW_SHOW)

GUIRegisterMsg($WM_COMMAND, "_WM_COMMAND")

While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
        Case $StartButton
            Start()
    EndSwitch
WEnd


Func Start()
    $bInterrupt = False
    GUICtrlSetState($StartButton, $GUI_DISABLE)
    GUICtrlSetState($EndButton, $GUI_ENABLE)
    While $bInterrupt = False
        Sleep(50)
        ConsoleWrite("again" & @CRLF)
    WEnd
    GUICtrlSetState($StartButton, $GUI_ENABLE)
    GUICtrlSetState($EndButton, $GUI_DISABLE)
    $bInterrupt = False
EndFunc   ;==>Start

Func _WM_COMMAND($hWnd, $Msg, $wParam, $lParam)
    ; The Stop button was pressed so set the flag
    If BitAND($wParam, 0x0000FFFF) = $EndButton Then
        $bInterrupt = True
    EndIf
    Return $GUI_RUNDEFMSG
EndFunc   ;==>_WM_COMMAND

The tutorial explains what is going on.

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

@Melba23,

How about if i changed the while loop with For loop

i managed to solve it with this

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

Global $Form1 = GUICreate("Form1", 623, 444, 192, 114)
Global $StartButton = GUICtrlCreateButton("Start", 88, 192, 171, 25)
Global $EndButton = GUICtrlCreateButton("Stop", 320, 192, 203, 25)
GUICtrlSetState($EndButton, $GUI_DISABLE)

GUISetState(@SW_SHOW)

GUIRegisterMsg($WM_COMMAND, "_WM_COMMAND")

Global $bInterrupt = False

While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
        Case $StartButton
            Start()
    EndSwitch
WEnd

Func Start()
    $bInterrupt = False
    GUICtrlSetState($StartButton, $GUI_DISABLE)
    GUICtrlSetState($EndButton, $GUI_ENABLE)

    For $x = 1 to 1000
        If $bInterrupt = False Then
            Sleep(500)
            ConsoleWrite("again " & $x & @CRLF)
        EndIf

    Next

    GUICtrlSetState($StartButton, $GUI_ENABLE)
    GUICtrlSetState($EndButton, $GUI_DISABLE)
    $bInterrupt = False
EndFunc   ;==>Start

Func _WM_COMMAND($hWnd, $Msg, $wParam, $lParam)
    ; The Stop button was pressed so set the flag
    If BitAND($wParam, 0x0000FFFF) = $EndButton Then
        $bInterrupt = True
    EndIf
    Return $GUI_RUNDEFMSG
EndFunc   ;==>_WM_COMMAND

Now, just wondering if you can do better with the for loop

Link to comment
Share on other sites

  • Moderators

TAMIRI,

I would have thought this better - you quit the For...Next loop immediately, whereas your version runs the loop for 1000 times regardless:

For $x = 1 to 1000
    If $bInterrupt Then
        ExitLoop
    Else
        Sleep(500)
        ConsoleWrite("again " & $x & @CRLF)
    EndIf
Next

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

×
×
  • Create New...