Jump to content

Interrupt Function, without loop present


Recommended Posts

OK, i'm trying to ID10T proof a script for accelerating a PC Swap. Everything is just about done before I can start testing. In previous versions I had a hotkey setup to interupt the script when I needed to cancel out of something. Now that it will probably be used by other people, I have been asked to give them some options. The problem that I currently have is interrupting a function via a button press (AKA 'Cancel' Button). So if a function is running they can hit the cancel button and it takes them back to the gui. Unfortunately there is only 1 loop in the script, and that is for the gui.

After a function is called the main GUI window disappears, and a smaller progress window is displayed. 'BusyBox()'. Previously the HOTKEY called 'Cleanup()', which exited the program. Now I have the HOTKEY calling 'Cancel()', which closes any running apps, and takes the user back to the GUI, but the function still runs in the backgroud. Is there a way to stop the function from running. Only the 'Check Size' and 'Browse' buttons work so I added the test button. Thanks in advance

Opt("TrayIconHide",1)

#AutoIt3Wrapper_Res_requestedExecutionLevel = requireAdministrator
#AutoIt3Wrapper_Icon = "\\namdco046\Shares\Columbia\Common To All - Temporary File Transfer Area\Mike Brown\z-Custom Tools\1-source files\greene's+g+icon.ico"
#AutoIt3Wrapper_OutFile = "\\namdco046\Shares\Columbia\Common To All - Temporary File Transfer Area\Mike Brown\z-Custom Tools\Refresh Tool v4.0.exe"

#include <Date.au3>
#include <ButtonConstants.au3>
#include <ComboConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <ProgressConstants.au3>
#include <SendMessage.au3>
#include <Constants.au3>

Global $Setup, $Busy, $Stage, $Busy, $gShare, $Share, $optFold, $Find, $TotSize, $FreeSpace, $SelSize, $Progress
Global $bRadio1, $bRadio2, $bRadio3, $rRadio1, $rRadio2, $rRadio3
Global $RefreshDir = @ScriptDir & "\z-refresh\"


$TotSize = Round(DirGetSize(@UserProfileDir)/1024/1024/1024, 2) + Round((DriveSpaceTotal("C:\") - DriveSpaceFree("C:\"))/1024, 2)

MainGUI()

Func MainGUI()
    ;Main GUI Window
    #Region ### START Koda GUI section ### Form=
    $Setup = GUICreate("Refresh Tool v4.0", 501, 501)
    $gUser = GUICtrlCreateLabel("Username: " & @UserName, 5, 10, 200, 17)
    $gCom = GUICtrlCreateLabel("Computer: " & @ComputerName, 8, 30, 200, 17)
    $Browse = GUICtrlCreateButton("Browse", 10, 55, 75, 37, $BS_DEFPUSHBUTTON)
    $ChkSze = GUICtrlCreateButton("Check" & @CRLF & " Size", 415, 55, 75, 37, $BS_MULTILINE)
    $optFold = GUICtrlCreateCheckbox("Add %username% to path", 90, 75, 137, 25)
    $NeedSpace = GUICtrlCreateLabel("Needed Space: " & $TotSize & " GB", 236, 10, 150, 17) ;234
        GUICtrlSetTip(-1, "This is approximately the amount of space needed to" & @CRLF & "successfully do a data transfer and Ghost image" & @CRLF & "!!!More Space will probably be needed!!!")
    $FreeSpace = GUICtrlCreateLabel("Free Space: ", 253, 30, 150, 17) ;253
    $gShare = GUICtrlCreateInput("", 90, 55, 320, 20)

    $Tab1 = GUICtrlCreateTab(0, 100, 499, 399)
        $TabSheet1 = GUICtrlCreateTabItem("Backup")
            $bTabTip = GUICtrlCreateLabel("Old Computer (Source PC)" & @CRLF & "- This is the computer I want to transfer files and settings from.", 9, 125, 541, 38)
            $bRadio1 = GUICtrlCreateRadio("WinXP to WinXP - Office 03/07", 35, 201, 200, 20)
            GUICtrlSetState(-1, $GUI_CHECKED)
            $bRadio2 = GUICtrlCreateRadio("WinXP to Win7 - Office 03/07", 35, 241, 200, 20)
            $bRadio3 = GUICtrlCreateRadio("Win7 to Win7 - Office 03/07", 35, 281, 200, 20)
            $bGhost = GUICtrlCreateButton("  Backup" & @CRLF & "Ghost Only", 16, 432, 100, 50, $BS_MULTILINE)
            $bDaGho = GUICtrlCreateButton("   Backup" & @CRLF & "Data-Ghost", 192, 432, 100, 50, $BS_MULTILINE)
            $bData = GUICtrlCreateButton("Backup" & @CRLF & "  Data", 377, 432, 100, 50, $BS_MULTILINE)
            $Test = GUICtrlCreateButton("TEST", 250, 200, 200, 200)

        $TabSheet2 = GUICtrlCreateTabItem("Restore")
            $rTabTip = GUICtrlCreateLabel("New Computer (Destination PC)" & @CRLF & "- This is the computer I want to transfer files and settings to.", 9, 125, 541, 38)
            $rRadio1 = GUICtrlCreateRadio("WinXP to WinXP - Office 03/07", 35, 201, 200, 20)
            GUICtrlSetState(-1, $GUI_CHECKED)
            $rRadio2 = GUICtrlCreateRadio("WinXP to Win7 - Office 03/07", 35, 241, 200, 20)
            $rRadio3 = GUICtrlCreateRadio("Win7 to Win7 - Office 03/07", 35, 281, 200, 20)
            $rGhost = GUICtrlCreateButton("  Restore" & @CRLF & "Ghost Only", 16, 432, 100, 50, $BS_MULTILINE)
            $rDaGho = GUICtrlCreateButton("   Restore" & @CRLF & "Data-Ghost", 192, 432, 100, 50, $BS_MULTILINE)
            $rData = GUICtrlCreateButton("Restore" & @CRLF & "  Data", 377, 432, 100, 50, $BS_MULTILINE)
    GUICtrlCreateTabItem("")

    GUISetState(@SW_SHOW)
    #EndRegion ### END Koda GUI section ###

    While 1
        $nMsg = GUIGetMsg()
        Switch $nMsg
            Case $GUI_EVENT_CLOSE
                Cleanup()
            Case $Browse
                SelFold()
            Case $ChkSze
                CheckSize()
            Case $bGhost
                Setup()
                GUICtrlSetData($Progress, 50)
                bGhost()
                GUICtrlSetData($Progress, 100)
                KillBox()
            Case $bDaGho
                Setup()
                GUICtrlSetData($Progress, 33)
                Backup()
                GUICtrlSetData($Progress, 66)
                bGhost()
                GUICtrlSetData($Progress, 100)
                KillBox()
            Case $bData
                Setup()
                GUICtrlSetData($Progress, 50)
                Backup()
                GUICtrlSetData($Progress, 100)
                KillBox()
            Case $rGhost
                Setup()
                GUICtrlSetData($Progress, 50)
                rGhost()
                GUICtrlSetData($Progress, 100)
                KillBox()
            Case $rDaGho
                Setup()
                GUICtrlSetData($Progress, 33)
                Restore()
                GUICtrlSetData($Progress, 66)
                rGhost()
                GUICtrlSetData($Progress, 100)
                KillBox()
            Case $rData
                Setup()
                GUICtrlSetData($Progress, 50)
                Restore()
                GUICtrlSetData($Progress, 100)
                KillBox()
            Case $Test
                Test()
                KillBox()
        EndSwitch
    WEnd
EndFunc

;Setup 'data store' and files for user with other functions
Func Setup()
    $Share = GUICtrlRead($gShare)
    If $Share = "" Then 
        MsgBox(0, "No Folder Selected", "You have not chosen a folder to save data to" & @CRLF & "Please choose a valid folder to save data to")
        Return
    ElseIf Not FileExists($Share) Then
        MsgBox(0, "No Folder Exists", "The folder you have chosen doesn't exist" & @CRLF & "Please choose a valid folder to save data to")
        Return
    ElseIf (FileExists($Share)) And Not (StringRight($Share,1) = "\") Then
        $Share = GUICtrlRead($gShare) & "\"
    EndIf
    
    If (FileExists($Share)) And (GUICtrlRead($optFold) = 1) Then
        $Share = $Share & @Username & "\"
    EndIf
    
    If Not FileExists($Share) Then DirCreate($Share)
        
    BusyBox()
    
    ;Install Files
    If Not (FileExists($RefreshDir)) Or Not (DirGetSize($RefreshDir) = 71685746) Then
        DirRemove($RefreshDir, 1)
        DirCreate($RefreshDir)
        GUICtrlSetData($Stage, "Copying Files")
        FileInstall("\\namdco046\Shares\Columbia\Common To All - Temporary File Transfer Area\Mike Brown\z-Custom Tools\1-source files\7z.exe", $RefreshDir)
        FileInstall("\\namdco046\Shares\Columbia\Common To All - Temporary File Transfer Area\Mike Brown\z-Custom Tools\1-source files\refreshv4.0.zip", $RefreshDir)
        
        GUICtrlSetData($Stage, "Expanding Files")
        RunWait('"' & $RefreshDir & "7z.exe" & '"' & " x -o"  & '"' & $RefreshDir  & '"' & " -y "  & '"' & $RefreshDir & "refreshv4.0.zip" & '"', "", @SW_HIDE)
    EndIf
EndFunc

;Backup routine
Func Backup()
    ;Copy saplogon.ini to backup folder
    FileCopy("c:\WINDOWS\saplogon.ini", $Share, 1)
    
    ;Copy LabelPrinter.ini to backup folder
    If FileExists("c:\windows\LabelPrinter.ini") Then FileCopy("c:\windows\LabelPrinter.ini", $Share, 1)
    
    ;Run CCleaner & close
    GUICtrlSetData($Stage, "Running CC Cleaner")
    RunWait('"' & $RefreshDir & "CCleaner\CCleaner.exe" & '"' & " /Auto")

    GUICtrlSetData($Stage, "Running USMT")
    
    ;Backup - XP-XP
    If GUICtrlRead($bRadio1) = 1 Then
        ;Windows Version Check
        If @OSVersion = "WIN_7" Then
            MsgBox(48, "!!!WARNING!!!", "You are currently running Windows 7" & @CRLF& "Your current settings will NOT work" & @CRLF& "Please make another Selection")
            KillBox()
            Return
        EndIf
        
        ;Previous Data Check
        If Not FileExists($Share & "1 XP-XP") Then DirCreate($Share & "1 XP-XP")
        If Not DirGetSize($Share & "1 XP-XP") = 0 Then
            $fold = MsgBox(4, "Data Exists", "USMT Data already exists" & @CRLF & "Do You want to overwrite data?")
            If $fold = 7 Then
                KillBox()
                Return
            Else
                DirRemove($Share & "1 XP-XP", 1)
                DirCreate($Share & "1 XP-XP")
            EndIf
        EndIf
   
        ;Run USMT
        $USMT = RunWait('"' & $RefreshDir & "USMT301-x86\ScanState.exe" & '"' & " " & '"' & $share & "1 XP-XP" & '"' & " /ue:*\* /ui:gracead\" & @UserName & " /config:config.xml /i:miguser.xml /i:migapp.xml /i:migsys.xml /i:custom.xml /i:map-print.xml /targetxp /v:13 /l:" & '"' & $share & "\" & @MON & @MDAY & @YEAR & "-" & @HOUR & @MIN & @SEC & "-XP-XP-scan.log" & '"', $RefreshDir & "USMT301-x86\")
        
    ;Backup - XP-7
    ElseIf GUICtrlRead($bRadio2) = 1 Then
        ;Windows Version Check
        If @OSVersion = "WIN_7" Then
            MsgBox(48, "!!!WARNING!!!", "You are currently running Windows 7" & @CRLF& "Your current settings will NOT work" & @CRLF& "Please make another Selection")
            KillBox()
            Return
        EndIf
        
        ;Previous Data Check
        If Not FileExists($Share & "2 XP-7") Then DirCreate($Share & "2 XP-7")
        If Not DirGetSize($Share & "2 XP-7") = 0 Then
            $fold = MsgBox(4, "Data Exists", "USMT Data already exists" & @CRLF & "Do You want to overwrite data?")
            If $fold = 7 Then
                KillBox()
                Return
            Else
                DirRemove($Share & "2 XP-7", 1)
                DirCreate($Share & "2 XP-7")
            EndIf
        EndIf
   
        ;Run USMT
        $USMT = RunWait('"' & $RefreshDir & "USMT401-x86\ScanState.exe" & '"' & " " & '"' & $share & "2 XP-7" & '"' & " /ue:*\* /ui:gracead\" & @UserName & " /config:config.xml /i:migdocs.xml /i:miguser.xml /i:migapp.xml /i:custom.xml /i:map-print.xml /c /localonly /v:13 /l:" & '"' & $share & "\" & @MON & @MDAY & @YEAR & "-" & @HOUR & @MIN & @SEC & "-XP-7-scan.log" & '"', $RefreshDir & "USMT401-x86\")
        
    ;Backup - 7-7
    ElseIf GUICtrlRead($bRadio3) = 1 Then
        ;Windows Version Check
        If @OSVersion = "WIN_XP" Then
            MsgBox(48, "!!!WARNING!!!", "You are currently running Windows XP" & @CRLF& "Your current settings will NOT work" & @CRLF& "Please make another Selection")
            KillBox()
            Return
        EndIf
        
        ;Previous Data Check
        If Not FileExists($Share & "3 7-7") Then DirCreate($Share & "3 7-7")
        If Not DirGetSize($Share & "3 7-7") = 0 Then
            $fold = MsgBox(4, "Data Exists", "Data already exists" & @CRLF & "Do You want to overwrite data?")
            If $fold = 7 Then
                KillBox()
                Return
            Else
                DirRemove($Share & "3 7-7", 1)
                DirCreate($Share & "3 7-7")
            EndIf
        EndIf
   
        ;Run USMT
        $USMT = RunWait('"' & $RefreshDir & "USMT401-x86\ScanState.exe" & '"' & " " & '"' & $share & "3 7-7" & '"' & " /ue:*\* /ui:gracead\" & @UserName & " /config:config.xml /i:migdocs.xml /i:miguser.xml /i:migapp.xml /i:custom.xml /i:map-print.xml /c /localonly /v:13 /l:" & '"' & $share & "\" & @MON & @MDAY & @YEAR & "-" & @HOUR & @MIN & @SEC & "-7-7-scan.log" & '"', $RefreshDir & "USMT401-x86\")
    EndIf
    
    If $USMT = 0 Then
        MsgBox(0,"Backup", "USMT was successful",5)
    Else
        MsgBox(0,"Backup", "USMT was NOT successful" & @CRLF & "Please check logs for errors")
    EndIf
    
EndFunc

;Restore Routine
Func Restore()

    ;Restore saplogon.ini (backup existing saplogon.ini)
    If FileExists("c:\WINDOWS\saplogon.ini") Then
        If FileExists("c:\WINDOWS\saplogon.ini.org") Then
            FileMove("c:\WINDOWS\saplogon.ini.org", "c:\WINDOWS\saplogon.ini.bak", 1)
            FileMove("c:\WINDOWS\saplogon.ini", "c:\WINDOWS\saplogon.ini.org", 1)
        Else
            FileMove("c:\WINDOWS\saplogon.ini", "c:\WINDOWS\saplogon.ini.org", 1)
        EndIf
    EndIf
    FileCopy($Share & "saplogon.ini", "c:\WINDOWS\saplogon.ini")

    ;Restore LabelPrinter.ini to backup folder
    If FileExists($Share & "LabelPrinter.ini") Then FileCopy($Share & "LabelPrinter.ini", "c:\windows\LabelPrinter.ini", 1)
    
    GUICtrlSetData($Stage, "Running USMT")
    
    ;Restore - XP-XP
    If GUICtrlRead($rRadio1) = 1 Then
    ;Windows Version Check
        If @OSVersion = "WIN_7" Then
            MsgBox(48, "!!!WARNING!!!", "You are currently running Windows 7" & @CRLF& "Your current settings will NOT work" & @CRLF& "Please make another Selection")
            KillBox()
            Return
        EndIf
        
        If Not (FileExists($Share & "1 XP-XP")) Or (DirGetSize($Share & "1 XP-XP") = 0) Then
            MsgBox(0,"Missing Data", "There is no data in the selected folder" & @CRLF & "Please Try Again")
            KillBox()
            Return
        EndIf
                
        ;Run USMT
        $USMT = RunWait('"' & $RefreshDir & "USMT301-x86\LoadState.exe" & '"' & " " & '"' & $Share & "1 XP-XP" & '"' & " /config:config.xml /i:miguser.xml /i:migapp.xml /i:migsys.xml /i:custom.xml /i:map-print.xml /v:13 /l:" & '"' & $share & "\" & @MON & @MDAY & @YEAR & "-" & @HOUR & @MIN & @SEC & "-XP-XP-load.log" & '"', $RefreshDir & "USMT301-x86\")
            
    ;Restore - XP-7
    ElseIf GUICtrlRead($rRadio2) = 1 Then
        ;Windows Version Check
        If @OSVersion = "WIN_XP" Then
            MsgBox(48, "!!!WARNING!!!", "You are currently running Windows XP" & @CRLF& "Your current settings will NOT work" & @CRLF& "Please make another Selection")
            KillBox()
            Return
        EndIf
        
        If Not (FileExists($Share & "2 XP-7")) Or (DirGetSize($Share & "2 XP-7") = 0) Then
            MsgBox(0,"Missing Data", "There is no data in the selected folder" & @CRLF & "Please Try Again")
            KillBox()
            Return
        EndIf
        
        ;Run USMT
        $USMT = RunWait('"' & $RefreshDir & "USMT401-x86\LoadState.exe" & '"' & " " & '"' & $Share & "2 XP-7" & '"' & " /config:config.xml /i:miguser.xml /i:migdocs.xml /i:migapp.xml /i:custom.xml /i:map-print.xml /v:13 /l:" & '"' & $share & "\" & @MON & @MDAY & @YEAR & "-" & @HOUR & @MIN & @SEC & "-XP-7-load.log" & '"', $RefreshDir & "USMT401-x86\")
            
    ;Restore - 7-7
    ElseIf GUICtrlRead($rRadio2) = 1 Then
        ;Windows Version Check
        If @OSVersion = "WIN_XP" Then
            MsgBox(48, "!!!WARNING!!!", "You are currently running Windows XP" & @CRLF& "Your current settings will NOT work" & @CRLF& "Please make another Selection")
            Return
        EndIf
        
        If Not (FileExists($Share & "3 7-7")) Or (DirGetSize($Share & "3 7-7") = 0) Then
            MsgBox(0,"Missing Data", "There is no data in the selected folder" & @CRLF & "Please Try Again")
            GUIDelete($Busy)
            GUISetState(@SW_SHOW, $Setup)
            Return
        EndIf
        
        ;Run USMT
        $USMT = RunWait('"' & $RefreshDir & "USMT401-x86\LoadState.exe" & '"' & " " & '"' & $Share & "3 7-7" & '"' & " /config:config.xml /i:miguser.xml /i:migdocs.xml /i:migapp.xml /i:custom.xml /i:map-print.xml /v:13 /l:" & '"' & $share & "\" & @MON & @MDAY & @YEAR & "-" & @HOUR & @MIN & @SEC & "-7-7-load.log" & '"', $RefreshDir & "USMT401-x86\")
    EndIf
    
    If $USMT = 0 Then
        MsgBox(0,"Restore", "USMT was successful" & @CRLF & "Please logoff user",10)
    Else
        MsgBox(0,"Restore", "USMT was NOT successful" & @CRLF & "Please check logs for errors")
    EndIf

EndFunc 

;Create Ghost Image
Func bGhost()
    GUICtrlSetData($Stage, "Creating Ghost Image")
    If Not FileExists($Share & "0 BACKUP") Then DirCreate($Share & "0 BACKUP")
    If Not DirGetSize($Share & "0 BACKUP") = 0 Then
        $fold = MsgBox(4, "Data Exists", "Data already exists" & @CRLF & "Do You want to overwrite data?")
        If $fold = 7 Then
            GUIDelete($Busy)
            GUISetState(@SW_SHOW, $Setup)
            Return
        Else
            DirRemove($Share & "0 BACKUP", 1)
            DirCreate($Share & "0 BACKUP")
        EndIf
    EndIf   
    
    ;Run Ghost
    $Ghost = RunWait('"' & $RefreshDir & "Ghost\Ghost32.exe" & '"' & " -clone,src=1,dst=" & '"' & $Share & "0 BACKUP\" & @ComputerName & ".gho" & '"' & ",mode=create -sure -span -split=2048 -z1")
    
    If $Ghost = 0 Then
        MsgBox(0,"Ghost32", "Ghost image was created",10)
    Else
        MsgBox(0,"Ghost32", "Ghost image was NOT created" & @CRLF & "Please check logs for errors")
    EndIf
EndFunc

;Restore Ghost Image
Func rGhost()
    GUICtrlSetData($Stage, "Restoring Ghost Image")
    Dim $Search[3] = ["", "BACKUP\", "0 BACKUP\"]
    For $i = 0 to 2 Step 1
        If FileExists($Share & $Search[$i] & "*.gho")Then
            $GhoImg = $Search[$i] & FileFindNextFile(FileFindFirstFile($Share & $Search[$i] & "*.gho"))
            ExitLoop
        Else
            If $i = 2 Then MsgBox (0, "Missing Data", "Ghost image not found")
            Return
        EndIf
    Next
    
    ;Run Ghost
    $Ghost = RunWait('"' & $RefreshDir & "Ghost\Ghost32.exe" & '"' & " -clone,mode=load,src=" & '"' & $Share & $GhoImg & '"' & ",dst=1 -sure")
    
    If $Ghost = 0 Then
        MsgBox(0,"Ghost32", "Ghost image was restored",10)
    Else
        MsgBox(0,"Ghost32", "Ghost image was NOT restored" & @CRLF & "Please check logs for errors")
    EndIf
EndFunc

;Select folder via GUI - 'Browse' button
Func SelFold()
    $Select = FileSelectFolder("Choose a Folder", "", 1)
    GUICtrlSetData($gShare, $Select)
    CheckSize()
EndFunc

;Check folder size - 'Check Size' Button
Func CheckSize()
    $SelSize = Round((DriveSpaceFree(GUICtrlRead($gShare))/1024), 2)
    GUICtrlSetData($FreeSpace, "Free Space: " & $SelSize & " GB")
    If ($TotSize + 500) >= $SelSize Then 
        MsgBox(48, "!!!WARNING!!!", "The selected folder DOES NOT have enough space to complete" & @CRLF & "a full backup (Data & Ghost Image)" & @CRLF & @CRLF & "Please select another folder")
        GUICtrlSetColor($FreeSpace, 0xff0000)
        EndIf
EndFunc

;Window displayed while application is doing something 
Func BusyBox()
    HotKeySet("{PAUSE}", "Cancel")
    $deskw = @DesktopWidth - 200
    $deskh = @DesktopHeight - 170
    GUISetState(@SW_HIDE, $Setup)
    Opt("GUIOnEventMode", 1)
    $Busy = GUICreate("PC Refresh-Running", 152, 152, $deskw, 50, $WS_EX_TOPMOST) ;152, 152, -1, -1, 0)
    $Stage = GUICtrlCreateLabel("", 25, 10, 100, 40, $SS_CENTER)
    $Progress = GUICtrlCreateProgress(2, 40, 142, 20, $PBS_SMOOTH)
    $Marquee = GUICtrlCreateProgress(2, 70, 142, 20, $PBS_MARQUEE)
    $Cancel = GUICtrlCreateButton("CANCEL", 38, 93, 70, 30)
    GUICtrlSetOnEvent(-1, "SendKill")
    _SendMessage(GUICtrlGetHandle($Marquee), $PBM_SETMARQUEE, True, 100)
    GUISetState(@SW_SHOW)
EndFunc

Func KillBox()
    HotKeySet("{PAUSE}")
    Opt("GUIOnEventMode", 0)
    GUIDelete($Busy)
    GUISetState(@SW_SHOW, $Setup)
EndFunc

Func Cancel()
    ProcessClose("7z.exe")
    ProcessClose("CCleaner.exe")
    ProcessClose("migwiz.exe")
    ProcessClose("ScanState.exe")
    ProcessClose("LoadState.exe")
    ProcessClose("ghost32.exe")
    KillBox()
EndFunc
    
;If HotKey is pressed, ask before exiting app
Func PreExit()
    $Escape = MsgBox(4, "", "Are you sure you want to quit?")
    If $Escape = 6 Then Cleanup()
EndFunc

;Cleanup when app is closed normally (closes any running programs)
Func Cleanup()
    ProcessClose("7z.exe")
    ProcessClose("CCleaner.exe")
    ProcessClose("migwiz.exe")
    ProcessClose("ScanState.exe")
    ProcessClose("LoadState.exe")
    ProcessClose("ghost32.exe")
    HotKeySet("{PAUSE}")
    Exit
EndFunc

Func SendKill()
    Send("{PAUSE}")
EndFunc

Func Test()
    BusyBox()
    For $i = 0 To 100 Step 10
        GUICtrlSetData($Progress, $i)
        Sleep(500)
    Next
EndFunc
Edited by AlchemistZim
Link to comment
Share on other sites

Hi AlchemistZim,

you could go and try switch your script to OnEventMode (Opt("GuiOneventMode", 1))

#include <GUIConstantsEx.au3>

Opt("GUIOnEventMode", 1)  ; Change to OnEvent mode
$mainwindow = GUICreate("Hello World", 200, 100)
GUISetOnEvent($GUI_EVENT_CLOSE, "CLOSEClicked")
GUICtrlCreateLabel("Hello world! How are you?", 30, 10)
$okbutton = GUICtrlCreateButton("OK", 70, 50, 60)
GUICtrlSetOnEvent($okbutton, "OKButton")
GUISetState(@SW_SHOW)

While 1
  Sleep(1000)  ; Idle around
WEnd

Func OKButton()
  ;Note: at this point @GUI_CTRLID would equal $okbutton,
  ;and @GUI_WINHANDLE would equal $mainwindow
;~   MsgBox(0, "GUI Event", "You pressed OK!")
    For $i = 0 To 100
        ConsoleWrite($i & @CRLF)
        Sleep(100)
    Next
EndFunc

Func CLOSEClicked()
  ;Note: at this point @GUI_CTRLID would equal $GUI_EVENT_CLOSE,
  ;and @GUI_WINHANDLE would equal $mainwindow
  MsgBox(0, "GUI Event", "You clicked CLOSE! Exiting...")
  Exit
EndFunc

Tested. Didn't work... :unsure:

Edited by Hannes123
Regards,Hannes[spoiler]If you can't convince them, confuse them![/spoiler]
Link to comment
Share on other sites

Hi AlchemistZim,

you could go and try switch your script to OnEventMode (Opt("GuiOneventMode", 1))

#include <GUIConstantsEx.au3>

Opt("GUIOnEventMode", 1)  ; Change to OnEvent mode
$mainwindow = GUICreate("Hello World", 200, 100)
GUISetOnEvent($GUI_EVENT_CLOSE, "CLOSEClicked")
GUICtrlCreateLabel("Hello world! How are you?", 30, 10)
$okbutton = GUICtrlCreateButton("OK", 70, 50, 60)
GUICtrlSetOnEvent($okbutton, "OKButton")
GUISetState(@SW_SHOW)

While 1
  Sleep(1000)  ; Idle around
WEnd

Func OKButton()
  ;Note: at this point @GUI_CTRLID would equal $okbutton,
  ;and @GUI_WINHANDLE would equal $mainwindow
;~   MsgBox(0, "GUI Event", "You pressed OK!")
    For $i = 0 To 100
        ConsoleWrite($i & @CRLF)
        Sleep(100)
    Next
EndFunc

Func CLOSEClicked()
  ;Note: at this point @GUI_CTRLID would equal $GUI_EVENT_CLOSE,
  ;and @GUI_WINHANDLE would equal $mainwindow
  MsgBox(0, "GUI Event", "You clicked CLOSE! Exiting...")
  Exit
EndFunc

Tested. Didn't work... :unsure:

I turn on OnEventMode while 'BusyBox' is on the screen, just for this reason. It works to the point of the MAIN GUI coming back, but when you try to start another function it waits for the previous function to finish.
Link to comment
Share on other sites

  • Moderators

AlchemistZim,

when you try to start another function it waits for the previous function to finish

And it always will because AutoIt queues events. :unsure:

Take a look at the Interrupting a running function tutorial in the Wiki and you will discover how to get over this limitation. :>

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

AlchemistZim,

And it always will because AutoIt queues events. :unsure:

Take a look at the Interrupting a running function tutorial in the Wiki and you will discover how to get over this limitation. :>

M23

Um, the tutorial is misleading IMO and doesn't include a very simple method which I often use. The tutorial reads like the following method won't work because it says that a function has to be completed before an interrupt can take effect but that isn't true. What it should say is that a function called by an interrupt cannot be interrupted (by another interrupt).

Press button Func_1 and then button Func_2 and you will see a function interrupted by an interrupt.

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


; Declare a flag
Global $Do1 = 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)
    if $Do1 then _Func_1B()
WEnd

Func _Func_1()
    $Do1 = true
EndFunc

func _Func_1B()
    For $i = 1 To 20
        ConsoleWrite("-Func 1 Running" & @CRLF)
        Sleep(100)
    Next
    ConsoleWrite(">Func 1 Ended" & @CRLF)
    Global $Do1 = false
EndFunc

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

Func _Exit()
    Exit
EndFunc
Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.
Link to comment
Share on other sites

  • Moderators

martin,

Thanks for that. I rarely use OnEvent mode and I did not realise that such a simple interrupt method existed when you were in that mode. :unsure:

I will amend the tutorial when I get the time this weekend. Thanks again. :>

M23

Edit: Done. ;)

Edited by Melba23

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