4

What function can I use to pause a script in AutoIt?

I know I can use sleep like this:

Func TogglePause()
    $Paused = NOT $Paused
    While $Paused
        sleep(100)
        ToolTip('Script is "Paused"',0,0)
    WEnd
    ToolTip("")
EndFunc

But is there a function for pause which has been implemented in AutoIt?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
JatSing
  • 4,857
  • 16
  • 55
  • 65
  • 5
    I don't understand what you expect from an in-built pause function. In AutoIt, the Sleep function is the Pause function that works for a set amount of time. In a loop, it works forever. What else should a pause function do? – Jos van Egmond Nov 07 '11 at 16:10

3 Answers3

6

I always used a hotkey and function to pause my script:

; Create a hot key. I'm using the "Pause/Break" key typically located near the number pad on the keyboard. The keycode for this key is simply "{PAUSE}"
HotKeySet("{PAUSE}", "togglePause")

; Create a Boolean (True of False variable) to tell your program whether or not your program is paused or unpaused. Set it equal to 'False' so the script is running by default.
Global $isPaused = False

; Create the togglePause function to stop/start the script
Func togglePause()
    ; When this function is initiated, the code on the next line 'toggles' the variable to True/False. If the script is unpaused (the $isPaused variable is set to 'False') then the next line will change it to 'True' and vice versa.
    $isPaused = Not $isPaused
    ; Create a while loop to stall the program
    ; The line below is the same thing as "While $isPaused == True"
    While $isPaused
        ; This code will run constantly until the $isPaused variable is set to 'True'. To make the script do nothing, simply add a sleep command.
        Sleep(100)
    WEnd
EndFunc

This can be done by pressing a hotkey (in this case Pause/Break) or by simply calling togglePause(). Example:

; Create a hot key. I'm using the "Pause/Break" key typically located near the number pad on the keyboard. The keycode for this key is simply "{PAUSE}"
HotKeySet("{PAUSE}", "togglePause")

; Create a Boolean (True of False variable) to tell your program whether or not your program is paused or unpaused. Set it equal to 'False' so the script is running by default.
Global $isPaused = False

MsgBox(0, "Unpaused", "You are able to see this because the program is unpaused!")

; CODE HERE!

MsgBox(0, "Pausing", "The script will automatically be paused once this dialog closes. Press the pause/break key to unpause!")
togglePause()
Sleep(500)
MsgBox(0, "Ta-Da!", "The script has been manually unpaused!")

; Create the togglePause function to stop/start the script
Func togglePause()
    ; When this function is initiated, the code on the next line 'toggles' the variable to True/False. If the script is unpaused (the $isPaused variable is set to 'False') then the next line will change it to 'True' and vice versa.
    $isPaused = Not $isPaused
    ; Create a while loop to stall the program
    ; The line below is the same thing as "While $isPaused == True"
    While $isPaused
        ; This code will run constantly until the $isPaused variable is set to 'True'. To make the script do nothing, simply add a sleep command.
        Sleep(100)
    WEnd
EndFunc
user4157124
  • 2,809
  • 13
  • 27
  • 42
Timothy Bomer
  • 504
  • 5
  • 21
3

Sleep already is the "pause"-function in AutoIt.

The only other method to pause the script is to click the trayicon in debug mode.

Daniel Jäger
  • 177
  • 1
  • 3
  • 13
3

You could always use the following method. I think it's the most efficient.

HotKeySet("{F8}", "StartScript")

While 1
While $script = True
MAIN SCRIPT HERE
    WEnd
WEnd

Func StartScript() ; Functions are normally placed last, making it easier to browse the code.

    If $script = False Then
        $script = True
        ToolTip("Runing|Press F8 to Stop",200,100,"Script Name",1,2)
        Sleep(2000)
        ToolTip("")
    Else
        $script = False
        ToolTip("Stoped|Press F8 to Start",200,100,"Script Name",1,2)
        Sleep(2000)
        ToolTip("")
    EndIf
EndFunc   ; ==> StartScript
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
spacing
  • 730
  • 2
  • 10
  • 41