Pause While loop by hotkey

a 夏天 提交于 2019-11-30 09:41:48

问题


I want to pause an AutoIt script containing a While loop and some functions. But I am only able to close the script on HotKeySet(). How can I pause it?

The script checks for changes on a part of screen (x,y coordinates are set in a config file) and takes screenshots after playing an alert sound. It doesn't stop the While loop when pushing the pause button. But closing the program works. Here is my code:

Global $Paused, $counter = 0
HotKeySet("{1}", "TogglePause")
HotKeySet("{2}", "Terminate")
HotKeySet("{3}", "ShowMessage")    

Init()
Start()
While 1
   $counter +=1
    ToolTip('Script is "Running"',0,0, $counter, 1)
    Sleep(700)
      Switch TrayGetMsg()
      Case $resume
      Start()
      DisableAlert()
      Case $exit
      ExitLoop
      Exit
    EndSwitch
 WEnd    

//some of the functions    
Func Start()
    $ready = 0
    $count = 0
    $lastScreenshotNum = 0
    TrayItemSetState($resume, $TRAY_DISABLE)
    TraySetIcon("on.ico")
    TakeScreenshot()
    AdlibRegister(TakeScreenshot,2000)
EndFunc    

Func Stop()
    AdlibUnRegister(TakeScreenshot)
    TraySetIcon("off.ico")
    TrayItemSetState($resume, $TRAY_ENABLE)
EndFunc

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

Func Terminate()
    Exit 0
EndFunc

Func ShowMessage()
    MsgBox(4096,"","This is a message.")
EndFunc

Func EnableAlert()
    SendMail()
    Alert()
    AdlibRegister(Alert,5000)
EndFunc

Func DisableAlert()
    AdlibUnRegister(Alert)
EndFunc

Func Alert()
    SoundPlay("alert.mp3")
EndFunc

回答1:


I want to pause an Autoit script, containing a while1 loop and some functions. But I am only able to close the the script on HotKeySet. So how can i pause it?

"Pause" While -loops by running their instructions conditional to (key-toggled) states (untested, no error checking):

Global Const $g_sKeyQuit    = 'q'
Global Const $g_sKeyPause   = 'p'
Global Const $g_iDelay      = 500

Global       $g_bStateQuit  = False
Global       $g_bStatePause = False

Main()

Func Main()
    HotKeySet($g_sKeyQuit, 'SwitchStateQuit')
    HotKeySet($g_sKeyPause, 'SwitchStatePause')

    While Not $g_bStateQuit
        If Not $g_bStatePause Then YourCode()
        Sleep($g_iDelay)
    WEnd

    Exit
EndFunc

Func YourCode()
    Local Static $iCount = 0
    $iCount += 1
    ConsoleWrite($iCount & @LF)
EndFunc

Func SwitchStateQuit()
    $g_bStateQuit = True
EndFunc

Func SwitchStatePause()
    _SwitchVar($g_sKeyPause)
EndFunc

Func _SwitchVar(ByRef $bSwitch)
    $bSwitch = Not $bSwitch
EndFunc
  • P pauses.
  • Q exits.
  • Change content of YourCode() as required.

Visual explanation (illustrating While -loop in Main()):

  • Loops and AdlibRegister() are different ways to accomplish the same (choose either one).
  • Use TimerDiff() if accurately timed repetition is required because simply adding Sleep() introduces time-drifts (disregards execution time, which is true for AdlibRegister() as well). As per documentation:

    Note that other running processes often affect the timing accuracy and so pauses are likely to last for slightly longer than requested.



来源:https://stackoverflow.com/questions/44162202/pause-while-loop-by-hotkey

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!