Jump to content
TAMIRI

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

Recommended Posts

TAMIRI

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 !

Share this post


Link to post
Share on other sites
Melba23

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

  • Thanks 1

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

 

Share this post


Link to post
Share on other sites
TAMIRI

wonderful job, thanks so much

Share this post


Link to post
Share on other sites
TAMIRI

@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

Share this post


Link to post
Share on other sites
Melba23

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

  • Like 1
  • Thanks 1

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

 

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

  • Similar Content

    • Vikramjeet
      By Vikramjeet
      #include <GuiConstantsEx.au3> $hGUI = GUICreate("CONTROL", 250, 100) GUICtrlCreateLabel("START or STOP", 57, 20, 150, 20) GUICtrlSetFont(3, 11, 2000) $StartService_Button = GUICtrlCreateButton("Start", 30, 55, 75, 25) $StopService_Button = GUICtrlCreateButton("Stop", 145, 55, 75, 25) GUISetState() While 1 $msg = GUIGetMsg() Switch $msg Case $GUI_EVENT_CLOSE Exit Case $StartService_Button _ServiceStartStop(1) Case $StopService_Button _ServiceStartStop() EndSwitch WEnd Func _ServiceStartStop($sFlag = 0) If $sFlag = 1 Then Run('"' & @AutoItExe & '" /AutoIt3ExecuteScript "C:\Users\Charlie\Desktop\Script1.au3"') Else ; How do I force stop Script 1 when I hit the STOP button EndIf EndFunc I have been able to create a Pop up with options 'START' and 'STOP'. I am able to start another script - Script1.au3 when I click the 'START' button. However I am not able to figure how to force stop Script1.au3 while it is running. My I please get ideas on how to stop Script1.au3 using the 'STOP' button. Thanks
×