调用Sleep()时用户界面无响应

时间:2015-06-05 01:35:11

标签: user-interface sleep autoit

Sleep()按下按钮时会出现问题。

我有一个If循环,每次鼠标移动/点击之间有随机延迟(最多10秒)。但是,当调用Sleep()时,脚本会停止响应其用户界面的“关闭”和“停止”按钮的单击:

#cs ----------------------------------------------------------------------------
    Author: JesterRM
    Website: N/A
#ce ----------------------------------------------------------------------------

#include <GUIConstantsEx.au3>
#include <MsgBoxConstants.au3>
#include <IE.au3>

;Comments On Information
;Set Mouse Position - MouseMove ( x, y)
;Mouse Click At Current Position - MouseClick ("Button")
;Wait Between Each Click Local $hTimer = TimerInit()>insert Link Break< Sleep(TIME)
;Random Sleep Time Sleep(Random(1000, 10000, 1)) = Random Sleep between 1 and 10 seconds
Opt('MustDeclareVars', 1)
MainGUI()

Func MainGUI()
    Local $Button1, $Button2, $Button3, $msg, $count, $OBJECT, $OBJECT_CTRL, $hTimer
    Global $count, $OBJECT, $OBJECT_CTRL

    $count = 0
    GUICreate("FREE KaW Bot V0.01", 1000, 650)
    ; Button Position and Names
    Opt("GUICoordMode", 2)
    $Button1 = GUICtrlCreateButton("Start", 370, 30, 100)
    $Button2 = GUICtrlCreateButton("Stop", 10, -1, 100)
    $Button3 = GUICtrlCreateButton("Close", 10, -1, 100)
    ;Create Internet Explorer in Window
    $OBJECT = ObjCreate("Shell.Explorer.2")
    $OBJECT_CTRL = GUICtrlCreateObj($OBJECT, -650, 20, 900, 550)
    GUISetState()
    _IECreateEmbedded()
    _IENavigate($OBJECT, "http://www.kingdomsatwar.com/play/")
    GUISetState()

    ; Run the GUI until the window is closed
    While 1
        $msg = GUIGetMsg()
        Select
            Case $msg = $GUI_EVENT_CLOSE
                ExitLoop
            ; Button PopUp Messages

            ; Start The Bot
            Case $msg = $Button1
                MsgBox(0, 'Start', 'Starting Bot in 10 Seconds after PopUp Closes')
                Sleep(10000)
                $count = 1
                If $count = 1 Then
                    Do
                        MouseMove(800, 100)
                        ; Begin The Timer and Store The Handle in a Variable
                        Local $hTimer = TimerInit()
                        ; Sleep For 3 Seconds
                        Sleep(Random(1000, 7000, 1))
                        Local $fDiff = TimerDiff($hTimer) ; Find the difference in time from the previous call of TimerInit. The variable we stored the TimerInit handlem is passed as the "handle" to TimerDiff.
                        MsgBox($MB_SYSTEMMODAL, "Time Difference", $fDiff)
                        MouseMove(200, 453)
                        ; Begin The Timer and Store The Handle in a Variable
                        Local $hTimer = TimerInit()
                        ; Sleep For 3 Seconds
                        Sleep(Random(1000, 7000, 1))
                        Local $fDiff = TimerDiff($hTimer) ; Find the difference in time from the previous call of TimerInit. The variable we stored the TimerInit handlem is passed as the "handle" to TimerDiff.
                        MsgBox($MB_SYSTEMMODAL, "Time Difference", $fDiff)
                    Until $count = 0
                Else
                    MsgBox(0, 'Stop', 'Stopping Bot')
                EndIf

            ; Stop The Bot
            Case $msg = $Button2
                MsgBox(0, 'Stop', 'Stopping Bot')
                $count = 0

            ; Close The Bot
            Case $msg = $Button3
                MsgBox(0, 'Close', 'Closing Bot')
                Exit
        EndSelect
    WEnd
EndFunc

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:2)

根据AutoIt Wiki - Interrupting a running function

  

AutoIt在OnEvent和MessageLoop模式下对函数调用进行排队。这意味着它等待一个函数完成并且代码重新进入你的空闲状态... WEnd循环然后运行下一个。

因此Sleep() 延迟处理GUI消息。解决方法是在可以中断的函数中包含Sleep()。例如:

Func _SleepInterrupted(ByRef $bStateInterrupt, Const $iTimeSleep, Const $iTimeInterrupt = 500)
    Local Const $iParts     = Ceiling($iTimeSleep / $iTimeInterrupt)
    Local Const $iRemainder = $iTimeSleep - ($iTimeInterrupt * Floor($iTimeSleep / $iTimeInterrupt))
    Local       $iCurrent   = $iTimeInterrupt
    Local       $iReturn    = 0

    For $i1 = 1 To $iParts

        If $bStateInterrupt Then
            $iReturn = 1
            ExitLoop 1
        EndIf

        If $i1 = $iParts And $iRemainder Then $iCurrent = $iRemainder
        Sleep($iCurrent)

    Next

    Return $iReturn
EndFunc
  • 如果(在此过程中的任何位置)$bStateInterrupt评估为True,该函数将立即返回1(忽略剩余时间)。如果不间断,它会返回0
  • $iTimeSleepSleep()定义持续时间(以毫秒为单位)。
  • $iTimeInterrupt定义了定期检查之间的延迟(以毫秒为单位)。

分解总时间可以在每个段之后处理OnEvent-和MessageLoop事件(如果$iTimeInterrupt = 500,则每0.5秒)。视觉(简化)解释:

SleepInterrupted()