Jump to content

Exit while loop active


Recommended Posts

Hello everyone,

 

I have below an example script. When I click button "Start" a variable will increment infinite number of times.

A second button "Exit", I want to use in order to close the script while the loop it's running, but it doesn't let me to do that, can exit only if I close from task manager.

 

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

Global $total = 1
Global $number

$Form1 = GUICreate("Form1", 246, 133, -1, -1)
$Button1 = GUICtrlCreateButton("START", 8, 8, 177, 33)
$Button2 = GUICtrlCreateButton("EXIT", 8, 48, 177, 33)
$Label1 = GUICtrlCreateLabel("NUMBER : " & $number, 8, 96, 178, 17)

GUISetState(@SW_SHOW, $Form1)

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

        Case $Button1
            For $i = 0 To $total
                $i = 0
                Sleep(100)
                $number += 1
                GUICtrlSetData($Label1, "NUMBER : " & $number)
            Next

        Case $Button2
            Exit

    EndSwitch
WEnd

 

Any idea how I can stop the script?

Thank you!

Link to comment
Share on other sites

  • Developers

Use the event mode for your buttons and logic like this:

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

Opt("GUIOnEventMode", 1) ;0=disabled, 1=OnEvent mode enabled

Global $total = 1
Global $number
Global $Start=0

$Form1 = GUICreate("Form1", 246, 133, -1, -1)
GUISetOnEvent($GUI_EVENT_CLOSE, "_Exit")
$Button1 = GUICtrlCreateButton("START", 8, 8, 177, 33)
GUICtrlSetOnEvent(-1,"_Start")
$Button2 = GUICtrlCreateButton("EXIT", 8, 48, 177, 33)
GUICtrlSetOnEvent(-1,"_Exit")
$Label1 = GUICtrlCreateLabel("NUMBER : " & $number, 8, 96, 178, 17)

GUISetState(@SW_SHOW, $Form1)

While sleep(50)
    if $Start Then
        For $i = 0 To $total
            $i = 0
            Sleep(100)
            $number += 1
            GUICtrlSetData($Label1, "NUMBER : " & $number)
        Next
    EndIf
WEnd

Func _START()
    $Start=1
EndFunc

Func _EXIT()
    Exit
EndFunc

Jos

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

  • Developers
5 minutes ago, FrancescoDiMuro said:

You could set up a button of your keyboard which, once pressed, stops the script :)

Something like:

While Not _IsPressed("SomeKey")
    ; Code
WEnd

 

This is not really using the Button to Stop/Exit and similar to using HotKeySet(). ;)

Jos

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

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