Sign in to follow this  
Followers 0
ziongates

Command to Exit any Running Function

6 posts in this topic

I have a GUI APP with buttons and a couple of functions. I need to assign the "Cancel" button a command that interrupts the "CopyMaster" function below (or any other looping function).

Basically: How do you kill a function or Several Functions via a button?

Func CopyMaster($Dest_Path,$Source_Path)

$Source = $Source_Path & "\*.*"

If FileExists($Source) AND (DirGetSize ( $Source ) > 0 ) Then DirMove($Source,$_Dest_Path)

EndFunc

Share this post


Link to post
Share on other sites



ziongates,

First thought is to use your "Cancel" button to set a global flag which you then check periodically within your functions. I do not believe there is any way of returning from a function other than by an internal decision of the function itself - although I am sure someone else knows a way and will tell us soon!

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______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

You can use Opt('GuiOnEventMode', 1) and register an event for the Cancel button that will change a global variable but this will prevent your script from using a msg loop.

Share this post


Link to post
Share on other sites

ziongates,

I have played about a bit and have a solution. You need to use HotKeySet which was in fact my very first thought, but I then discarded it! Only this function will "break in" to set the flag - from the Help file: "A hotkey-press *typically* interrupts the active AutoIt function/statement and runs its user function until it completes or is interrupted".

So look at this:

#include <GUIConstantsEx.au3>

Opt("GUIOnEventMode", 1)

Global $fCancel = False

GUICreate("Test", 200,200)
GUISetOnEvent($GUI_EVENT_CLOSE, "On_Exit")

GUICtrlCreateButton("GUI", 10, 10, 80, 30)
    GUICtrlSetOnEvent(-1, "On_GUI")
    
GUICtrlCreateButton("Looper", 10, 85, 80, 30)
    GUICtrlSetOnEvent(-1, "On_Loop")
    
Global $Label = GUICtrlCreateLabel("", 100, 85, 50, 30)

HotKeySet("{SPACE}", "On_Cancel")
    
Global $State = GUICtrlCreateLabel($fCancel, 10, 160, 50, 30)

GUISetState()

While 1
    Sleep(100)
WEnd

Func On_Exit()
    Exit
EndFunc

Func On_Loop()
    
    $count = 0
    While $count < 100
        Sleep (1000)
        If $fCancel = True Then
            $fCancel = False
            GUICtrlSetData($State, $fCancel)
            GUICtrlSetData($Label, "")
            Return
        EndIf
        $count += 1
        GUICtrlSetData($Label, $count)
    WEnd

EndFunc


Func On_GUI()
    
    $NewGUI = GUICreate("New GUI", 100, 100, 100, 100)
    GUISetState()
    
    Opt("GUIOnEventMode", 0)
    
    While 1
        If GUIGetMsg() = -3 Then
            GUIDelete($NewGUI)
            Opt("GUIOnEventMode", 1)
            Return
        EndIf 
    WEnd
    
EndFunc
    
Func On_Cancel()
    
    $fCancel = True
    GUICtrlSetData($State, $fCancel)
    
EndFunc

If you press "Looper" the count begins and will not stop until you press the "HOT KEY" - the spacebar in this case.

I hate to contradict Authenticity, but you can use both GUI modes in the same script - just not at the same time! If you press "GUI" you display a small window with its own GUIGetMsg loop - you have to reset the Opt("GUIOnEventMode") of course. However, once you are in this loop, the OnEvent "Looper" button does not work. The HotKey does though!

I hope this is useful.

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______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

hey can't u like put the function in a Do..Until loop?? and put the condition of button pressed for the until expression?? I haven't tried this.. but can u do it??


[font="Garamond"]Manjish Naik[/font]Engineer, Global Services - QPSHoneywell Automation India LimitedE-mail - Manjish.Naik@honeywell.com

Share this post


Link to post
Share on other sites

Thanks for all the responses, I'm investigating the "global variable check" option.

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
Sign in to follow this  
Followers 0