2017-05-24 213 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

    注意,其他正在運行的進程往往會影響定時精度,因此暫停可能持續略長要求。