Jump to content

Stopping a Loop from another button


perccapt
 Share

Recommended Posts

I have a GUI with a stop and start button. The Start button starts a loop but I want the Stop button to be able to stop the loop after it starts. It seems that It just goes into a permanent loop and doesn't look at anything else but what's going on in the loop.

#include <GUIConstants.au3>
$Form1 = GUICreate("AForm1", 340, 312, 192, 125)
$Start = GUICtrlCreateButton("Start", 64, 16, 81, 73, 0)
$Stop = GUICtrlCreateButton("Stop", 152, 16, 81, 73, 0)
$Edit1 = GUICtrlCreateEdit("", 24, 104, 289, 177)
GUICtrlSetData($Edit1, "")
GUISetState(@SW_SHOW)
Dim $i = 2
While 1
    $msg = GuiGetMsg()
    Select
    Case $msg = $GUI_EVENT_CLOSE
        ExitLoop
    Case $msg = $Start
        Do
            GUICtrlSetData($Edit1,"Test"&@crlf,1)
            Sleep(1000)
        Until $i <>2
    Case $msg = $Stop
        $i = 3
        Case Else
    ;
    EndSelect
WEnd
Exit
Link to comment
Share on other sites

here's one approach...

#include <GUIConstants.au3>

Dim $i, $t

$Form1 = GUICreate("AForm1", 340, 312, 192, 125)
$Start = GUICtrlCreateButton("Start", 64, 16, 81, 73, 0)
$Stop = GUICtrlCreateButton("Stop", 152, 16, 81, 73, 0)
$Edit1 = GUICtrlCreateEdit("", 24, 104, 289, 177)
GUICtrlSetData($Edit1, "")
GUISetState(@SW_SHOW)

While 1
    $msg = GUIGetMsg()
    Select
        Case $msg = $GUI_EVENT_CLOSE
            ExitLoop
        Case $msg = $Start
            GUICtrlSetData($Edit1, "Test" & @CRLF, 1)
            $i = 1
        Case $msg = $Stop
            $i = 0
        Case Else
            ;
    EndSelect
    If $i Then
        $t = $t + 1
        If $t = 100 Then
            $t = 0
            GUICtrlSetData($Edit1, "Test" & @CRLF, 1)
        EndIf
    EndIf
WEnd
Exit

8)

NEWHeader1.png

Link to comment
Share on other sites

Here's a way you could do it using events.

#include <GUIConstants.au3>
$Form1 = GUICreate("AForm1", 340, 312, 192, 125)
$Start = GUICtrlCreateButton("Start", 64, 16, 81, 73, 0)
$Stop1 = GUICtrlCreateButton("Stop", 152, 16, 81, 73, 0)
$Edit1 = GUICtrlCreateEdit("", 24, 104, 289, 177)
GUICtrlSetData($Edit1, "")
GUISetState(@SW_SHOW)

Opt("GUIOnEventMode", 1);to use events instaed of guigetmsg

GUICtrlSetOnEvent($stop1,'stopcount')
GUICtrlSetOnEvent($start,'startit')
GUISetOnEvent($GUI_EVENT_CLOSE, "enditall")
Global $i = 0
$looptimer = TimerInit()

Do 
    if $i = 2 and TimerDiff($looptimer) >= 1000 then
      GUICtrlSetData($Edit1,"Test"&@crlf,1)
      $looptimer = TimerInit()
    EndIf
Until $i = 99
    
Exit


Func enditall()
    $i = 99
EndFunc

Func startit()
       $i = 2
       $looptimer = TimerInit()
EndFunc

Func stopcount()
    $i = 3
EndFunc
Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.
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...