Jump to content

Problem with exiting function after Interrupt


Recommended Posts

Lets say we had this code which lets button 2 interrupt button 1. What if I wanted when after clicking button 2, it stops 1 completely and button 1's function never resumes until I click it again. How would it be done?

#include <GUIConstantsEx.au3>

; Declare a flag
Global $fRunOne = False

Opt("GUIOnEventMode", 1)

$hGUI = GUICreate("Test", 500, 500)
GUISetOnEvent($GUI_EVENT_CLOSE, "_Exit")

$hButton_1 = GUICtrlCreateButton("Func One", 10, 10, 80, 30)
GUICtrlSetOnEvent($hButton_1, "_Func_1")
$hButton_2 = GUICtrlCreateButton("Func Two", 10, 50, 80, 30)
GUICtrlSetOnEvent($hButton_2, "_Func_2")

GUISetState()

While 1
     Sleep(10)
     ; Check if the flag has been set by the OnEvent function
     If $fRunOne Then
         ; Now start the "real" function from within the main code
         _Func_1_Run()
     EndIf
WEnd

Func _Func_1()
     ; Set the flag within the OnEvent function
     $fRunOne = True
EndFunc   ;==>_Func_1

Func _Func_1_Run()
     For $i = 1 To 20
         ConsoleWrite("-Func 1 Running" & @CRLF)
         Sleep(100)
     Next
     ConsoleWrite(">Func 1 Ended" & @CRLF)
     Global $fRunOne = False
EndFunc   ;==>_Func_1_Run

Func _Func_2()
     For $i = 1 To 3
         ConsoleWrite("+Func 2 Running" & @CRLF)
         Sleep(100)
     Next
     ConsoleWrite(">Func 2 Ended" & @CRLF)
EndFunc   ;==>_Func_2

Func _Exit()
     Exit
EndFunc   ;==>_Exit
Link to comment
Share on other sites

  • Moderators

Ronald,

Welcome to the AutoIt forum. :)

I would do it like this:

#include <GUIConstantsEx.au3>

; Declare a flag
Global $fRunOne = False

Opt("GUIOnEventMode", 1)

$hGUI = GUICreate("Test", 500, 500)
GUISetOnEvent($GUI_EVENT_CLOSE, "_Exit")

$hButton_1 = GUICtrlCreateButton("Func One", 10, 10, 80, 30)
GUICtrlSetOnEvent($hButton_1, "_Func_1")
$hButton_2 = GUICtrlCreateButton("Func Two", 10, 50, 80, 30)
GUICtrlSetOnEvent($hButton_2, "_Func_2")

GUISetState()

While 1
    Sleep(10)
    ; Check if the flag has been set by the OnEvent function
    If $fRunOne Then
        ; Now start the "real" function from within the main code
        _Func_1_Run()
    EndIf
WEnd

Func _Func_1()
    ; Set the flag within the OnEvent function
    $fRunOne = True
EndFunc   ;==>_Func_1

Func _Func_1_Run()
    For $i = 1 To 20
        ConsoleWrite("-Func 1 Running" & @CRLF)
        If $fRunOne = False Then ; Check if the flag is cleared <<<<<<<<<<<<<<<<<<<<<<<<
            ConsoleWrite(">Func 1 Ended Prematurely" & @CRLF)
            Return
        EndIf
        Sleep(100)
    Next
    ConsoleWrite(">Func 1 Ended" & @CRLF)
    Global $fRunOne = False
EndFunc   ;==>_Func_1_Run

Func _Func_2()
    $fRunOne = False ; Clear the flag <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
    For $i = 1 To 3
        ConsoleWrite("+Func 2 Running" & @CRLF)
        Sleep(100)
    Next
    ConsoleWrite(">Func 2 Ended" & @CRLF)
EndFunc   ;==>_Func_2

Func _Exit()
    Exit
EndFunc   ;==>_Exit

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

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