通过热键暂停 While 循环
Pause While loop by hotkey
我想暂停包含 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
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
-循环 运行 他们的指令条件为(键切换)状态(未经测试,无错误检查):
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()
中的循环):
- 循环和
AdlibRegister()
是完成相同任务的不同方法(任选其一)。
- 使用
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
循环和一些函数的 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
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
-循环 运行 他们的指令条件为(键切换)状态(未经测试,无错误检查):
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()
中的循环):
- 循环和
AdlibRegister()
是完成相同任务的不同方法(任选其一)。 - 使用
TimerDiff()
if accurately timed repetition is required because simply addingSleep()
introduces time-drifts (disregards execution time, which is true forAdlibRegister()
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.