Sign in to follow this  
Followers 0
jfornango

Prematurely exit a function

3 posts in this topic

Hi all.  Been using AutoIT for a few years now, and I love it.  Also, the forum has been a valuable resource.  Thanks for all the community support. Still, I know that my coding skills aren't quite up to snuff

I'm working on a project and could use some assistance on a "best approach" for an issue I've come across.  I didn't see an article that really applied, but if there is one, just point me to it.

Here's the score, I'm writing an application that will be used to migrate a user's data between two computers. (don't ask why my client isn't using roaming profiles or the profile migration utility. Too much customization for either to work.)

Anyway, the backup and restore processes include roughly a dozen steps each.  During the entire process, I want the deskside technician to be able to hit a Cancel button, and have the process stop. (this is like an emergency stop)  Previous code that I have written is usually a sigle step repeated ad-nauseum, and just uses an GUIGetMsg() to check for an button, which then kills the program with an exit.  However, I need to do some house-keeping, including logging the fact that the run was interupted by the user, before terminating.

What would be the best approach to accomplish this?

Thank you in advance for any and all recommendations.

Share this post


Link to post
Share on other sites



jfornango,

Welcome to the AutoIt forum. :)

I suggest you read the Interrupting a running function tutorial in the Wiki - that shows a number of ways of doing what you want to do. :)

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

#3 ·  Posted (edited)

Ok, so after reading this, it looks like I'm already using one of the methods described. As an example, here are the first two pieces of my backup process.

; copy the user's desktop
    $strStatus = "Backing up desktop"
    GUICtrlSetData ($lblStatus, "Status: " & $strStatus)
    GUISetState()
    $rc = DirCopy(@UserProfileDir & "\Desktop", $strTarget & "\Desktop")
    If $rc = 1 Then
        FileWriteLine($strProcessLog, _NowTime() & "  Desktop copied successfully")
    Else
        FileWriteLine($strProcessLog, _NowTime() & "  There was an error " & _
                    "copying the Desktop. Error code " & @error)
    EndIf

    ; check for the Cancel button
    $ctrl_msg = GUIGetMsg()
    if $ctrl_msg = $btnCancel Then
        $strStatus1 = "Cancel"
        _dataCancel()
    EndIf

    ; copy the user's IE Favorites
    $strStatus = "Backing up IE Favorites"
    GUICtrlSetData ($lblStatus, "Status: " & $strStatus)
    GUISetState()
    $rc = DirCopy(@UserProfileDir & "\Favorites", $strTarget & "\Favorites")
    If $rc = 1 Then
        FileWriteLine($strProcessLog, _NowTime() & "  Favorites copied successfully")
    Else
        FileWriteLine($strProcessLog, _NowTime() & "  There was an error " & _
                    "copying the Favorites. Error code " & @error)
    EndIf

    ; check for the Cancel button
    $ctrl_msg = GUIGetMsg()
    if $ctrl_msg = $btnCancel Then
        $strStatus1 = "Cancel"
        _dataCancel()
    EndIf

And for reference, here is the Cancel function

Func _dataCancel()

    Select
        Case $strStatus1 = "Idle"
            MsgBox(0, "ALERT", "There is currently no operation to cancel.")
        Case $strStatus1 = "Backup"
            $strStatus = "Canceling...  Please Wait"
            GUICtrlSetData($lblStatus, $strStatus)
            GUISetState()
            FileWriteLine($strProcessLog, _NowTime() & "Backup cancelled by user.")
            FileClose($strProcessLog)
            Exit
        Case $strStatus1 = "Restore"
            $strStatus = "Canceling...  Please Wait"
            GUICtrlSetData($lblStatus, $strStatus)
            GUISetState()
            FileWriteLine($strProcessLog, _NowTime() & "Restore cancelled by user.")
            FileClose($strProcessLog)
            Exit
    EndSelect

EndFunc

So, you can see where I am checking for a "cancel" message after each piece of the process. This is works well and coded fairly easily.  So far, so good.

Here's my problem.  What if I discover that a user has a 20 GB folder on their desktop after the backup has started (which makes me want to stop and evaluate the state of the backup location)?  Is there a way to interupt the DirCopy command without actually closing the program?  I need to be able to update the log file, to show that the process was manually interupted (instead of, say, crashing), and at what point.

Edited by jfornango

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