2017-05-24 249 views
1

我想暂停包含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

你TogglePause功能确实暂停截图,但是你错过了回切换。如果不是$暂停,则添加,然后在TogglePause结束时开始()。有用。 – Milos

回答

1

我想暂停AutoIt脚本,包含while1环路和一些功能。但我只能关闭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() 

     EndIf 

     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()的内容。

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

Conditional While-loop

  • 循环和AdlibRegister()有不同的方法来完成相同的(选择的任一个)。
  • 使用TimerDiff()如果需要准确定时重复因为简单地增加Sleep()引入时间漂移(无视执行时间,这对于AdlibRegister()以及真)。按documentation

    注意,其他正在运行的进程往往会影响定时精度,因此暂停可能持续略长要求。