Jump to content

Can you exit a function like ExitLoop?


Recommended Posts

I'm wondering if I can exit from a calling(parent) function (as in, exit from the current function and the one that called it), like you can exit a parent Loop with the "ExitLoop 2" command. If not, I'd like to know if there is a more efficient way of exiting a function without adding several "if, then, return" statements in my code.

I'm using _IELoadWait($oIE) and WinWaitActive() commands throughout my code. I've had to add timeouts to all of these commands because I found that they were causing my code to hang indefinitely whenever something unexpected occured. In several of these cases, a timeout means I need to exit the function (so that my script isn't trying to click buttons that don't exist) and then proceed to the next process. Do I have to add the following code to every single Wait command in order to achieve this?

If IELoadWait($oIE, 0, 30000) > 0 Then
     Return
EndIf

I know it's only an extra 2-3 lines, but when I've got several of these checks in a function, the code becomes less readable and means I'm typing the same thing over and over. I wanted to create a seperate function that would be able to handle the check and automatically break out of the function, but I quickly realized that the Return command would only break out of that function, then go right back and continue running the parent function. I tried the Exit command, but that just exited the entire script.

Edited by AmbiguousJoe
Link to comment
Share on other sites

What you have is the correct way to do it. If you want the readability of the code to flow better then you could use the single line version of the IF statement.

If IELoadWait($oIE, 0, 30000) Then Return
Edited by Bowmore

"Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to build bigger and better idiots. So far, the universe is winning."- Rick Cook

Link to comment
Share on other sites

If you want the readability of the code to flow better then you could use the single line version of the IF statement.

For the record, the above is a matter for opinion.

Edited by JohnOne

AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

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...