通过热键暂停 While 循环 [英] Pause While loop by hotkey

查看:36
本文介绍了通过热键暂停 While 循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想暂停一个包含 While 循环和一些函数的 AutoIt 脚本.但我只能在 HotKeySet() 上关闭脚本.我怎样才能暂停它?

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

全局 $Paused, $counter = 0HotKeySet("{1}", "TogglePause")HotKeySet("{2}", "终止")HotKeySet("{3}", "ShowMessage")在里面()开始()虽然 1$计数器 +=1ToolTip('脚本正在运行"',0,0, $counter, 1)睡眠(700)切换 TrayGetMsg()案例 $resume开始()禁用警报()案例 $exit退出循环出口结束开关结束//部分函数函数开始()准备好 = 0$count = 0$lastScreenshotNum = 0TrayItemSetState($resume, $TRAY_DISABLE)TraySetIcon("on.ico")截屏()AdlibRegister(TakeScreenshot,2000)结束函数功能停止()AdlibUnRegister(截屏)TraySetIcon("off.ico")TrayItemSetState($resume, $TRAY_ENABLE)结束函数功能切换暂停()停止()$Paused = 不是 $Paused虽然 $Paused睡眠(100)ToolTip('脚本被暂停"',0,0, $counter, 1)结束工具提示("")结束函数函数终止()退出 0结束函数函数 ShowMessage()MsgBox(4096,"","这是一条消息.")结束函数函数 EnableAlert()发邮件()警报()AdlibRegister(Alert,5000)结束函数Func DisableAlert()AdlibUnRegister(警报)结束函数功能警报()SoundPlay("alert.mp3")结束函数

解决方案

我想暂停一个 Autoit 脚本,其中包含一个 while1 循环和一些函数.但我只能关闭 HotKeySet 上的脚本.那么我该如何暂停呢?

"Pause" While -通过以(键切换)状态(未测试,无错误检查)为条件运行指令来循环:

全局常量 $g_sKeyQuit = 'q'全局常量 $g_sKeyPause = 'p'全局常量 $g_iDelay = 500全局 $g_bStateQuit = False全局 $g_bStatePause = False主要的()函数主()HotKeySet($g_sKeyQuit, 'SwitchStateQuit')HotKeySet($g_sKeyPause, 'SwitchStatePause')虽然不是 $g_bStateQuit如果不是 $g_bStatePause 那么 YourCode()睡眠($g_iDelay)结束出口结束函数Func YourCode()本地静态 $iCount = 0$iCount += 1ConsoleWrite($iCount & @LF)结束函数函数 SwitchStateQuit()$g_bStateQuit = 真结束函数Func SwitchStatePause()_SwitchVar($g_sKeyPause)结束函数Func _SwitchVar(ByRef $bSwitch)$bSwitch = 不是 $bSwitch结束函数

  • P 停顿.
  • Q 退出.
  • 根据需要更改 YourCode() 的内容.

视觉解释(说明 While -Main() 中的循环):

  • Loops 和 AdlibRegister() 是完成相同任务的不同方式(选择其中一种).
  • 使用TimerDiff()如果需要准确定时重复,因为简单地添加Sleep()会引入时间漂移(忽略执行时间,这对于AdlibRegister()来说是正确的好).根据文档:<块引用>

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

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

解决方案

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.

这篇关于通过热键暂停 While 循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆