Pause While loop by hotkey

前端 未结 1 436
孤城傲影
孤城傲影 2020-12-22 04:30

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

相关标签:
1条回答
  • 2020-12-22 04:52

    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.

    0 讨论(0)
提交回复
热议问题