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

User interface becomes unresponsive when calling Sleep()

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

我有一个 If 循环,每只鼠标之间有随机延迟(最多 10 秒)move/-click。但是,当调用 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

我该如何解决这个问题?

根据AutoIt Wiki - Interrupting a running function

AutoIt queues function calls in both OnEvent and MessageLoop modes. This means that it waits until a function is finished and the code is back in your idle While...WEnd loop before running the next.

因此 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,该函数将立即 return 1(忽略剩余时间)。它 returns 0 如果不间断。
  • $iTimeSleepSleep().
  • 定义 持续时间(以毫秒为单位)
  • $iTimeInterrupt 定义定期检查之间的延迟(以毫秒为单位)。

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