如果Then语句循环

时间:2014-03-09 21:05:53

标签: autoit

我正在创建一个脚本,用于打开程序,登录并执行特定任务。 我创建了一个带有变量add和loopin的登录脚本。 问题出在程序上,有时它只会给你一个连接错误或网络问题。 我需要的是,如果脚本遇到问题,它将自行重置并通过它得到错误的帐户,这是我到目前为止,我只是无法使其工作

$t = 0
$n = "account"
$p = "password"

For $r = X To X
    Run("program")

    AutoITSetOption("MouseCoordMode", 0)
    AutoITSetOption("WinTitleMatchMode", 3)

    Do
        Sleep(1000)
        $t = $t + 1
    Until WinActive("program") Or $t = 15

    $t = 0

    Sleep(1500)
    Send("{TAB}")
    Sleep(100)
    Send("{TAB}")
    Sleep(100)
    Send("{Enter}")
    Sleep(100)
    Send($n & $r)
    Sleep(200)
    Send("{TAB}")
    Sleep(200)
    Send($p & $r)
    Sleep(100)
    Send("{Enter}")
    Sleep(5500)

    If $t > 14 Then
        $r = $r - 1
        Run(@ComSpec & " /c taskkill /F /im program.exe")

        Do
            Sleep(500)
            $t = $t + 1
        Until WinActive("Program - Update News") Or $t = 15

        $t = 0
        WinActivate("Program")
        Sleep(2000)
        MouseClick("Primary", 28, 12)
        Sleep(1000)
        MouseClick("Primary", 35, 125)
        Sleep(1000)
        MouseClick("Primary", 360, 175)
        Sleep(2000)
        Send("{ENTER}")
        Sleep(2500)

        Run(@ComSpec & " /c taskkill /F /im program.exe")
    ; EndIf
Next

现在它所做的是重新运行程序而不实际关闭错误窗口

1 个答案:

答案 0 :(得分:1)

希望这能让您更好地了解如何创建脚本并解决问题。评论是为了让您了解所采取的步骤。

#include <msgboxconstants.au3>

Local $t = 0, $n = "account", $p = "password" ; $n & $p are your own data
Local $r = ["X", "X", "X"], $e, $msg, $w  ;$r is filled with your own data

AutoItSetOption("MouseCoordMode", 0)
AutoItSetOption("WinTitleMatchMode", 3)

HotKeySet("{ESC}", "Quit") ;If you want to exit the script, press the ESC key
Sender()  ;call the function to run

While 1
    If $e <> 0 Then   ;if @error is set to a non-zero variable, wait 1 sec (1000 millisec) and run again
        Sleep(1000)
        Sender()
    ElseIf $w = 0 Then  ;if msgbox value is 0 AND retry OR ignore has been pressed, wait 1 second and run again
        Sleep(1000)
        $w += 1
        Sender()
    EndIf
WEnd


Func Sender()   ;wrapped in function to make returning out of the function easier
    For $r = "X" To UBound($r)  ; for the first value in $r to the length of the array

        Run("program") 
        $e = @error  ;if Run sets @error then put @error and check the value with an if statement
        If $e <> 0 Then
            $msg = MsgBox(2, "ERROR", "error running program, exiting...")
            If $msg = $IDABORT Then  ;if we don't want to run the program again we abort
                Quit()
            ElseIf $msg = $IDRETRY Then ;if we want to retry then we hit retry
                Return
            ElseIf $msg = $IDIGNORE Then ;if we want to retry then we hit ignore as well
                Return ;go out of the function because we have recieved an error and want to restart the function
            EndIf
        EndIf
        SetError(0) ;sets @error to 0 so that we don't accidentally restart our program 
        Do
            Sleep(1000)
            $t += 1  ;add 1 to $t
            Sleep(1500)
            Send("{TAB}")
            Sleep(100)
            Send("{TAB}")
            Sleep(100)
            Send("{Enter}")
            Sleep(100)
            Send($n & $r)
            Sleep(200)
            Send("{TAB}")
            Sleep(200)
            Send($p & $r)
            Sleep(100)
            Send("{Enter}")
            Sleep(5500)
            If $t > 14 Then
                $r -= 1 ;minus 1 from $r
                Run(@ComSpec & " /c taskkill /F /im program.exe")
                $e = @error
                If $e <> 0 Then
                    $msg = MsgBox(2, "ERROR", "error running " & @ComSpec & " /c taskkill /F /im program.exe, Abort to Quit; Retry & Ignore to RETRY.")
                If $msg = $IDABORT Then
                    Quit()
                ElseIf $msg = $IDRETRY Then
                    Return
                ElseIf $msg = $IDIGNORE Then
                    Return
                EndIf
            EndIf
        EndIf
        SetError(0)
    Until WinActive("program") Or $t = 15

    $t = 0   ;set $t to 0

    Do
        Sleep(500)
        $t += 1
        $w = WinActivate("Program")
        If $w = 0 Then
            $msg = MsgBox(2, "ERROR", "error activating program, Abort to Quit; Retry & Ignore to RETRY.")
            If $msg = $IDABORT Then
                Quit()
            ElseIf $msg = $IDRETRY Then
                Return
            ElseIf $msg = $IDIGNORE Then
                Return
            EndIf
        EndIf
        $w += 1
        Sleep(2000)
        MouseClick("Primary", 28, 12)
        Sleep(1000)
        MouseClick("Primary", 35, 125)
        Sleep(1000)
        MouseClick("Primary", 360, 175)
        Sleep(2000)
        Send("{ENTER}")
        Sleep(2500)
        Run(@ComSpec & " /c taskkill /F /im program.exe")
        $e = @error
        If $e <> 0 Then
            $msg = MsgBox(2, "ERROR", "error running " & @ComSpec & " /c taskkill /F /im program.exe, Abort to Quit; Retry & Ignore to RETRY.")
            If $msg = $IDABORT Then
                Quit()
            ElseIf $msg = $IDRETRY Then
                Return
            ElseIf $msg = $IDIGNORE Then
                Return
            EndIf
        EndIf
        SetError(0)
    Until WinActive("Program - Update News") Or $t = 15

Next

EndFunc   ;==>Sender

Func Quit() ;will be called when we want to completely stop the script
    Exit
EndFunc   ;==>Quit