Jump to content

How to interrupt and restart loop within function? (GoTo would work :( )


 Share

Recommended Posts

I think I have ways to make this work, but they require rewriting a bunch of code and I'd like to see if there is a simple solution that I can't see. This function is checking the status of a service on a remote machine using psservice. I'm doing it this way because I couldn't find something existing that would do this AND accept credentials for the remote machine. Sometimes it takes a while to return so I'd like to put a timer in and if it takes longer than 2 seconds just jump back to the top and try again without exiting the function. A simple GoTo would work but it doesn't exist and is taboo. The solution isn't obvious to me. I'd love to know if anyone has an idea. Thanks!

Func serviceStatus($c)
    Dim $s = 0
    $result = Run(@ComSpec & " /c psservice.exe \\" & $c & " -u <Username> -p <Password> query <ServiceName>", @SystemDir, @SW_HIDE, $STDOUT_CHILD)
    While 1
    $line = StdoutRead($result) 
    if @error then ExitLoop
        if $line <> "" then
            if stringmid($line, StringInStr($line, "STATE") + 11,1) = 4 Then ;parse line to find status
                $s = "Running"
            EndIf
            ExitLoop
        EndIf
    Wend
    StdioClose($result)
    Return $s
EndFunc
Link to comment
Share on other sites

GoTo is not in AutoIt any more because it will result in a mess, even used by the best. If you want to recall your function, then just do so inside of it.

Func serviceStatus($c)
    Dim $s = 0
    $result = Run(@ComSpec & " /c psservice.exe \\" & $c & " -u <Username> -p <Password> query <ServiceName>", @SystemDir, @SW_HIDE, $STDOUT_CHILD)
    While 1
    $line = StdoutRead($result) 
    if @error then ExitLoop
        if $line <> "" then
            if stringmid($line, StringInStr($line, "STATE") + 11,1) = 4 Then ;parse line to find status
                $s = "Running"
            EndIf
            ExitLoop
        EndIf
    Wend
    ; Want to call the function here?
    ; servceStatus("val")
    StdioClose($result)
    Return $s
EndFunc
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...