暂停按热键循环

时间:2017-05-24 15:04:18

标签: while-loop autoit pause

我想暂停一个包含While循环和一些函数的AutoIt脚本。但我只能关闭HotKeySet()上的脚本。我怎么能暂停呢?

脚本检查屏幕的一部分上的更改(x,y坐标在配置文件中设置)并在播放警报声后截取屏幕截图。按下暂停按钮时,它不会停止While循环。但关闭程序是有效的。这是我的代码:

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 个答案:

答案 0 :(得分:1)

  

我想暂停一个包含while1循环和一些函数的Autoit脚本。但我只能关闭HotKeySet上的脚本。那我怎么能暂停呢?

“暂停”While -loops通过以(键切换)状态为条件运行指令(未经测试,无错误检查):

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 暂停。
  • Q 退出。
  • 根据需要更改YourCode()的内容。

视觉解释(在While中说明Main() -loop):

Conditional While-loop

  • 循环和AdlibRegister()是完成相同操作的不同方法(选择其中之一)。
  • 如果准确需要定时重复,请使用TimerDiff(),因为只需添加Sleep()会引入时间漂移(忽略执行时间,AdlibRegister()也是如此)。根据{{​​3}}:
      

    请注意,其他正在运行的进程通常会影响计时准确性,因此暂停的持续时间可能比请求的时间略长。

相关问题