Jump to content

How to exit this loop?


Recommended Posts

Click start and it starts counting. When you click stop then it reacts and disable/enable buttons. But I dont know how to exit that loop or even whole func.

Code:

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


global $fInterrupt = 0

$Form1 = GUICreate("Form1", 374, 184, 192, 124)
$start = GUICtrlCreateButton("start", 32, 32, 161, 49)
$stop = GUICtrlCreateButton("stop", 32, 96, 161, 49)
$Label1 = GUICtrlCreateLabel("Label1", 216, 72, 92, 17)
GUISetState(@SW_SHOW)

GUIRegisterMsg($WM_COMMAND, "_WM_COMMAND")
GUIRegisterMsg($WM_SYSCOMMAND, "_WM_SYSCOMMAND")

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $start
            GUICtrlSetState($start, $GUI_DISABLE)
            GUICtrlSetState($Stop, $GUI_ENABLE)
            core()
    EndSwitch
WEnd



Func core()
AdlibRegister("_Interrupt",2000)

Local $i = 0


While 1

$i = $i + 1
Sleep(1000)
GUICtrlSetData($Label1,$i)
Wend


Endfunc






Func _Interrupt()
    If $fInterrupt Then
        Switch $fInterrupt
            Case 1
                    GUICtrlSetState($start, $GUI_ENABLE)
                    GUICtrlSetState($Stop, $GUI_DISABLE)

                $fInterrupt = 0
                Return True
            Case 2
                Exit
        EndSwitch
    EndIf
    Return False
EndFunc   ;==>_Interrupt

Func _WM_COMMAND($hWnd, $Msg, $wParam, $lParam)
    If BitAND($wParam, 0x0000FFFF) = $Stop Then $fInterrupt = 1
    Return $GUI_RUNDEFMSG
EndFunc

Func _WM_SYSCOMMAND($hWnd, $Msg, $wParam, $lParam)
    ; Closure [X} pressed
    If BitAND($wParam, 0xFFF0) = 0xF060 Then
        $fInterrupt = 2
    EndIf
    Return $GUI_RUNDEFMSG
EndFunc
Link to comment
Share on other sites

Not the most elegant, but works:

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


global $fInterrupt = 0

$Form1 = GUICreate("Form1", 374, 184, 192, 124)
$start = GUICtrlCreateButton("start", 32, 32, 161, 49)
$stop = GUICtrlCreateButton("stop", 32, 96, 161, 49)
$Label1 = GUICtrlCreateLabel("Label1", 216, 72, 92, 17)
GUISetState(@SW_SHOW)

GUIRegisterMsg($WM_COMMAND, "_WM_COMMAND")
GUIRegisterMsg($WM_SYSCOMMAND, "_WM_SYSCOMMAND")

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $start
            GUICtrlSetState($start, $GUI_DISABLE)
            GUICtrlSetState($stop, $GUI_ENABLE)
            core()
    EndSwitch
WEnd

Func core()
    AdlibRegister("_Interrupt", 100)
    Local $i = 0
    While 1
        If ControlCommand($Form1, "", $start, "IsEnabled") Then Return
        $i = $i + 1
        Sleep(1000)
        GUICtrlSetData($Label1, $i)
    Wend
Endfunc   ;==>core


Func _Interrupt()
    If $fInterrupt Then
        Switch $fInterrupt
            Case 1
                GUICtrlSetState($start, $GUI_ENABLE)
                GUICtrlSetState($stop, $GUI_DISABLE)

                $fInterrupt = 0
                Return True
            Case 2
                Exit
        EndSwitch
    EndIf
    Return False
EndFunc   ;==>_Interrupt

Func _WM_COMMAND($hWnd, $Msg, $wParam, $lParam)
    If BitAND($wParam, 0x0000FFFF) = $stop Then $fInterrupt = 1
    Return $GUI_RUNDEFMSG
EndFunc   ;==>_WM_COMMAND

Func _WM_SYSCOMMAND($hWnd, $Msg, $wParam, $lParam)
    ; Closure [X} pressed
    If BitAND($wParam, 0xFFF0) = 0xF060 Then
        $fInterrupt = 2
    EndIf
    Return $GUI_RUNDEFMSG
EndFunc   ;==>_WM_SYSCOMMAND
IEbyXPATH-Grab IE DOM objects by XPATH IEscriptRecord-Makings of an IE script recorder ExcelFromXML-Create Excel docs without excel installed GetAllWindowControls-Output all control data on a given window.
Link to comment
Share on other sites

Sorry, forgot to mention that I want to stop lopp/ exit func without writing code into it.

If writing code into it then best way is to put   if _Interrupt then exitloop

 

The problem is that I have too much code which I need to stop. I need to put in like 50 lines "if _Interrupt then exitloop".

I want to do it easer.

Link to comment
Share on other sites

I think this worked. Making While $loop = 1 instead of While 1 and when button clicked then it makes $loop = 0. So it stops. This is good way to do that?

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


global $fInterrupt = 0, $loop = 1

$Form1 = GUICreate("Form1", 374, 184, 192, 124)
$start = GUICtrlCreateButton("start", 32, 32, 161, 49)
$stop = GUICtrlCreateButton("stop", 32, 96, 161, 49)
$Label1 = GUICtrlCreateLabel("Label1", 216, 72, 92, 17)
GUISetState(@SW_SHOW)

GUIRegisterMsg($WM_COMMAND, "_WM_COMMAND")
GUIRegisterMsg($WM_SYSCOMMAND, "_WM_SYSCOMMAND")

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $start
            GUICtrlSetState($start, $GUI_DISABLE)
            GUICtrlSetState($Stop, $GUI_ENABLE)
            core()
    EndSwitch
WEnd



Func core()
AdlibRegister("_Interrupt",2000)

Local $i = 0


While $loop = 1

$i = $i + 1
Sleep(1000)
GUICtrlSetData($Label1,$i)
Wend


Endfunc









Func _Interrupt()
    If $fInterrupt Then
        Switch $fInterrupt
            Case 1
                    GUICtrlSetState($start, $GUI_ENABLE)
                    GUICtrlSetState($Stop, $GUI_DISABLE)
                     $loop = 0
                $fInterrupt = 0
                Return True
            Case 2
                Exit
        EndSwitch
    EndIf
    Return False
EndFunc   ;==>_Interrupt

Func _WM_COMMAND($hWnd, $Msg, $wParam, $lParam)
    If BitAND($wParam, 0x0000FFFF) = $Stop Then $fInterrupt = 1
    Return $GUI_RUNDEFMSG
EndFunc

Func _WM_SYSCOMMAND($hWnd, $Msg, $wParam, $lParam)
    ; Closure [X} pressed
    If BitAND($wParam, 0xFFF0) = 0xF060 Then
        $fInterrupt = 2
    EndIf
    Return $GUI_RUNDEFMSG
EndFunc
Edited by AntiFros
Link to comment
Share on other sites

  • Moderators

AntoFros,

Why so complicated? For something this simple just keep the code simple too: ;)

#include <GUIConstantsEx.au3>

Global $fRun = False

$Form1 = GUICreate("Form1", 374, 184, 192, 124)
$start = GUICtrlCreateButton("start", 32, 32, 161, 49)
$stop = GUICtrlCreateButton("stop", 32, 96, 161, 49)
GUICtrlSetState($stop, $GUI_DISABLE)
$Label1 = GUICtrlCreateLabel("0", 216, 72, 92, 17)
GUISetState(@SW_SHOW)

While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
        Case $start
            GUICtrlSetState($start, $GUI_DISABLE)
            GUICtrlSetState($stop, $GUI_ENABLE)
            $nBegin = TimerInit()
            $fRun = True
        Case $stop
            GUICtrlSetState($start, $GUI_ENABLE)
            GUICtrlSetState($stop, $GUI_DISABLE)
            $fRun = False
    EndSwitch

    If $fRun And TimerDiff($nBegin) > 1000 Then
        GUICtrlSetData($Label1, GUICtrlRead($Label1) + 1)
        $nBegin = TimerInit()
    EndIf

WEnd
All clear? :)

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

Ahh can't explain with my bad english. Timer in my code is just easy way to see if loop still works or not.

The last code I posted works but loop stops after it finished everything what is in it.

I need to put something here

Func _Interrupt()
    If $fInterrupt Then
        Switch $fInterrupt
            Case 1
                GUICtrlSetState($start, $GUI_ENABLE)
                GUICtrlSetState($stop, $GUI_DISABLE)
  HERE/////////////////////////////////////////////////////////////
                $fInterrupt = 0
                Return True
            Case 2
                Exit
        EndSwitch
    EndIf
    Return False
EndFunc   ;==>_Interrupt

Code which would end loop in core() or stop whole core() func.

Edited by AntiFros
Link to comment
Share on other sites

are you trying to exit from the while - wend loop within the core() function by an "external" event?

Edited by Chimp

 

image.jpeg.9f1a974c98e9f77d824b358729b089b0.jpeg Chimp

small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

Link to comment
Share on other sites

  • Moderators

AntiFros,

How about this script then: :)

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

Global $fInterrupt = 0

$Form1 = GUICreate("Form1", 374, 184, 192, 124)
$start = GUICtrlCreateButton("start", 32, 32, 161, 49)
$stop = GUICtrlCreateButton("stop", 32, 96, 161, 49)
GUICtrlSetState($stop, $GUI_DISABLE)
$Label1 = GUICtrlCreateLabel("Label1", 216, 72, 92, 17)
GUISetState(@SW_SHOW)

GUIRegisterMsg($WM_COMMAND, "_WM_COMMAND")
GUIRegisterMsg($WM_SYSCOMMAND, "_WM_SYSCOMMAND")

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $start
            GUICtrlSetState($start, $GUI_DISABLE)
            GUICtrlSetState($stop, $GUI_ENABLE)
            core()
    EndSwitch
WEnd

Func core()
    Local $i = 1, $nBegin = TimerInit()
    While 1

        If TimerDiff($nBegin) > 1000 Then
            $i = $i + 1
            GUICtrlSetData($Label1, $i)
            $nBegin = TimerInit()
        EndIf

        If $fInterrupt Then
            Switch $fInterrupt
                Case 1
                    $fInterrupt = False
                    GUICtrlSetState($start, $GUI_ENABLE)
                    GUICtrlSetState($stop, $GUI_DISABLE)
                    Return
                Case 2
                    Exit
            EndSwitch
        EndIf

        Sleep(10)

    WEnd


EndFunc   ;==>core

Func _WM_COMMAND($hWnd, $Msg, $wParam, $lParam)
    If BitAND($wParam, 0x0000FFFF) = $stop Then $fInterrupt = 1
    Return $GUI_RUNDEFMSG
EndFunc   ;==>_WM_COMMAND

Func _WM_SYSCOMMAND($hWnd, $Msg, $wParam, $lParam)
    ; Closure [X} pressed
    If BitAND($wParam, 0xFFF0) = 0xF060 Then
        $fInterrupt = 2
    EndIf
    Return $GUI_RUNDEFMSG
EndFunc   ;==>_WM_SYSCOMMAND
Any better? :huh:

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

are you trying to exit from the while - wend loop within the core() function by an "external" event?

Im trying to exit whole func. If I stop loop then func ends and it stops. So also I can directly exitfunc.

 Didnt understood half of your sentence but yes, Im trying to exit func outside it. Without any code in core() func.

 

 

Melba, nope. Ill probably just put "If _Interrupt() then exitloop" in every second line.

If there would be func like   ExitFunc(core)... :king:

Edited by AntiFros
Link to comment
Share on other sites

  • Moderators

AntiFros,

In your original script, the core function had a loop - which is why I used one. If there is no loop then you are poorly placed to interrupt the function. Can you give us an idea of its internal structure - perhaps we might be able to suggest something. :)

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

Ahh can't explain with my bad english. Timer in my code is just easy way to see if loop still works or not.

The last code I posted works but loop stops after it finished everything what is in it.

I need to put something here

Func _Interrupt()
    If $fInterrupt Then
        Switch $fInterrupt
            Case 1
                GUICtrlSetState($start, $GUI_ENABLE)
                GUICtrlSetState($stop, $GUI_DISABLE)
  HERE/////////////////////////////////////////////////////////////
                $fInterrupt = 0
                Return True
            Case 2
                Exit
        EndSwitch
    EndIf
    Return False
EndFunc   ;==>_Interrupt

Code which would end loop in core() or stop whole core() func.

 

Please use code tags when you post your code.

Read "How to post code on the forum" from my singature.

mLipok

Signature beginning:
Please remember: "AutoIt"..... *  Wondering who uses AutoIt and what it can be used for ? * Forum Rules *
ADO.au3 UDF * POP3.au3 UDF * XML.au3 UDF * IE on Windows 11 * How to ask ChatGPT for AutoIt Codefor other useful stuff click the following button:

Spoiler

Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind. 

My contribution (my own projects): * Debenu Quick PDF Library - UDF * Debenu PDF Viewer SDK - UDF * Acrobat Reader - ActiveX Viewer * UDF for PDFCreator v1.x.x * XZip - UDF * AppCompatFlags UDF * CrowdinAPI UDF * _WinMergeCompare2Files() * _JavaExceptionAdd() * _IsBeta() * Writing DPI Awareness App - workaround * _AutoIt_RequiredVersion() * Chilkatsoft.au3 UDF * TeamViewer.au3 UDF * JavaManagement UDF * VIES over SOAP * WinSCP UDF * GHAPI UDF - modest begining - comunication with GitHub REST APIErrorLog.au3 UDF - A logging Library * Include Dependency Tree (Tool for analyzing script relations) * Show_Macro_Values.au3 *

 

My contribution to others projects or UDF based on  others projects: * _sql.au3 UDF  * POP3.au3 UDF *  RTF Printer - UDF * XML.au3 UDF * ADO.au3 UDF SMTP Mailer UDF * Dual Monitor resolution detection * * 2GUI on Dual Monitor System * _SciLexer.au3 UDF * SciTE - Lexer for console pane

Useful links: * Forum Rules * Forum etiquette *  Forum Information and FAQs * How to post code on the forum * AutoIt Online Documentation * AutoIt Online Beta Documentation * SciTE4AutoIt3 getting started * Convert text blocks to AutoIt code * Games made in Autoit * Programming related sites * Polish AutoIt Tutorial * DllCall Code Generator * 

Wiki: Expand your knowledge - AutoIt Wiki * Collection of User Defined Functions * How to use HelpFile * Good coding practices in AutoIt * 

OpenOffice/LibreOffice/XLS Related: WriterDemo.au3 * XLS/MDB from scratch with ADOX

IE Related:  * How to use IE.au3  UDF with  AutoIt v3.3.14.x * Why isn't Autoit able to click a Javascript Dialog? * Clicking javascript button with no ID * IE document >> save as MHT file * IETab Switcher (by LarsJ ) * HTML Entities * _IEquerySelectorAll() (by uncommon) * IE in TaskSchedulerIE Embedded Control Versioning (use IE9+ and HTML5 in a GUI) * PDF Related:How to get reference to PDF object embeded in IE * IE on Windows 11

I encourage you to read: * Global Vars * Best Coding Practices * Please explain code used in Help file for several File functions * OOP-like approach in AutoIt * UDF-Spec Questions *  EXAMPLE: How To Catch ConsoleWrite() output to a file or to CMD *

I also encourage you to check awesome @trancexx code:  * Create COM objects from modules without any demand on user to register anything. * Another COM object registering stuffOnHungApp handlerAvoid "AutoIt Error" message box in unknown errors  * HTML editor

winhttp.au3 related : * https://www.autoitscript.com/forum/topic/206771-winhttpau3-download-problem-youre-speaking-plain-http-to-an-ssl-enabled-server-port/

"Homo sum; humani nil a me alienum puto" - Publius Terentius Afer
"Program are meant to be read by humans and only incidentally for computers and execute" - Donald Knuth, "The Art of Computer Programming"
:naughty:  :ranting:, be  :) and       \\//_.

Anticipating Errors :  "Any program that accepts data from a user must include code to validate that data before sending it to the data store. You cannot rely on the data store, ...., or even your programming language to notify you of problems. You must check every byte entered by your users, making sure that data is the correct type for its field and that required fields are not empty."

Signature last update: 2023-04-24

Link to comment
Share on other sites

Is this anything like the functionality you desire?

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

_Main()

Func _Main()
    ConsoleWrite("_Main()" & @LF)
    While 3
        _func1()
        Sleep(2000)
    WEnd
EndFunc

Func _func1()
    ConsoleWrite("_func1()" & @LF)
    _func2()
EndFunc

Func _func2()
    ConsoleWrite("_func2()" & @LF)
EndFunc

Func _HotFunc()
    _Main()
EndFunc

Recursion is not always bad when used wisely, but I'd not advice it in a tight loop or if it's something which will be used very often in your script.

AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Link to comment
Share on other sites

Ok, shortened my main code to show what Im doing here. Im collecting data about player (talking about game called Battlefield 2. This is NOT a game automation). This code are showing me if player is online, if he got any score and so  on. Just faster way to see what I can see also from surfing web.

#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <IE.au3>
#include <array.au3>
global $fInterrupt = 0

$oIE = _IECreate("http://37.187.75.29/bf2sclone/")

$Form1 = GUICreate("Form1", 374, 184, 192, 124)
$start = GUICtrlCreateButton("start", 32, 32, 161, 49)
$stop = GUICtrlCreateButton("stop", 32, 96, 161, 49)
$Label1 = GUICtrlCreateLabel("Label1", 216, 72, 92, 17)
GUISetState(@SW_SHOW)

GUIRegisterMsg($WM_COMMAND, "_WM_COMMAND")
GUIRegisterMsg($WM_SYSCOMMAND, "_WM_SYSCOMMAND")

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $start
            GUICtrlSetState($start, $GUI_DISABLE)
            GUICtrlSetState($Stop, $GUI_ENABLE)
            core()
    EndSwitch
WEnd



Func core()


_IELinkClickByText($oIE,"Scarred1.50")
Sleep(3000)
Local $oTable = _IETableGetCollection($oIE,0)
Local $aTableData = _IETableWriteToArray($oTable)
;_ArrayDisplay($aTableData)
While 1
; there code refreshes page every 1-5 mins (random)
;and gathers information.
;So if you quit loop then func ends. 
;there is about hundret lines of code mainly gathering tables, sleeps and _ielinkclicks. So I need easy way to stop it by pressing button.
Wend

Endfunc






Func _Interrupt()
    If $fInterrupt Then
        Switch $fInterrupt
            Case 1
                    GUICtrlSetState($start, $GUI_ENABLE)
                    GUICtrlSetState($Stop, $GUI_DISABLE)

                $fInterrupt = 0
                Return True
            Case 2
                Exit
        EndSwitch
    EndIf
    Return False
EndFunc   ;==>_Interrupt

Func _WM_COMMAND($hWnd, $Msg, $wParam, $lParam)
    If BitAND($wParam, 0x0000FFFF) = $Stop Then $fInterrupt = 1
    Return $GUI_RUNDEFMSG
EndFunc

Func _WM_SYSCOMMAND($hWnd, $Msg, $wParam, $lParam)
    ; Closure [X} pressed
    If BitAND($wParam, 0xFFF0) = 0xF060 Then
        $fInterrupt = 2
    EndIf
    Return $GUI_RUNDEFMSG
EndFunc

Again, this is NOT game automation lol.

 

----Edit

JohnOne, where is button? lol.

Edited by AntiFros
Link to comment
Share on other sites

Obviously there is no button, what I have shown is a quick example of what functionality you might need.

It shows that it will leave any function and any loop anywhere in your script and return to it's main function.

AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Link to comment
Share on other sites

another simple example:
you should declare a global variable (as you already done) for example global $fInterrupt = 0

then close all the functions that you want interrupt from the "outside" in a loop like this:

While Not $fInterrupt 
                   ; your code goes here
    WEnd

so when you want to exit just set the $fInterrupt to true (in this example by a checkbox)

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

Global $fInterrupt = 0, $loop = 1
AdlibRegister("_Interrupt", 500) ; check every half second if stop the loop(s)

$Form1 = GUICreate("Form1", 374, 184, 192, 124)
$start = GUICtrlCreateButton("start", 32, 32, 161, 49)
; $stop = GUICtrlCreateButton("stop", 32, 96, 161, 49)
$Label1 = GUICtrlCreateLabel("Label1", 216, 72, 92, 17)
Global $iCheckbox = GUICtrlCreateCheckbox("interrupt", 10, 10, 185, 25)

GUISetState(@SW_SHOW)
While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $start
            GUICtrlSetState($start, $GUI_DISABLE)
            core() ; this starts the loop in the core() function
            GUICtrlSetState($start, $GUI_ENABLE)
            GUICtrlSetState($iCheckbox, $GUI_UNCHECKED)
    EndSwitch
WEnd

Func core()
    Local $i = 0
    While Not $fInterrupt ; $i$loop = 1
        $i = $i + 1
        Sleep(1000)
        GUICtrlSetData($Label1, $i)
    WEnd
EndFunc   ;==>core

Func _Interrupt()
    $fInterrupt = GUICtrlRead($iCheckbox) <> 4
EndFunc   ;==>_Interrupt
Edited by Chimp

 

image.jpeg.9f1a974c98e9f77d824b358729b089b0.jpeg Chimp

small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

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