Jump to content

Help Please: How to break a while loop?


imNew
 Share

Recommended Posts

my apologies, the question should be: how to break a while loop IN THE MIDDLE?

I have main loop running (let's say, "moving 100 boxes from A to B"), and I have a "AdlibEnable("_error_occured")" routine running at the same time to monitor interrupts.

I wanted that: every time an interrupt happens and _error_occured is called, the main loop(suppose it is lifting the 46th box from A and prepare to fly to B, ) will NOT continue to run from where it was interrupted but reset itself to run from the start again (the main loop will not continue to fly to B, but go back to A and relift the 46th box).

any help, suggestions will much appreciated thanks.

Edited by moxinghbian
Link to comment
Share on other sites

Global $gBreak = 0
AdLibEnable("AddLibFunc", 1000)

Do
  Sleep(250)
Until $gBreak = 1
Exit
Func AddLibFUnc()
    $gBreak = 1
EndFunc

Link to comment
Share on other sites

THanks for the replies, but I don't think that's exactly what I am looking for

my scenario is:

Global $gBreak = 0
AdLibEnable("AddLibFunc", 1000)

Do
  Sleep(250)
;==================
;some 500 lines of code goes in here
;==================
Until $gBreak = 1
Exit

Func AddLibFUnc()
    $gBreak = 1
EndFunc

setting gBreak = 1 will only break the loop after the loop finished all the 500 lines of code

so what I really would like is: if I am in the middle of that 500lines of code, I want to skip the rest of the 500lines and go to the starting point of the loop.

I can only find exitLoop and continueLoop, but they only work at the point where I put them into the 500lines of code. if I want to loop to restart anytime I want, I have to put an exitLoop in front of every 500lines of statement.

Edited by moxinghbian
Link to comment
Share on other sites

what is a "teleporter"? but something like "teleporter" or "goto" would be nice

I think my case would be a pretty common scenario:

my scrip deals with some application, let's say filling a table. if the application crashed in the middle, my AdLibenable would pick up that and restart the application. After the Adlibenable finishes, I would like to restart the whole process to deal with that application.

Link to comment
Share on other sites

No actually your request is a big NONONO HELL NO in any language. It is good damn the worst practice you ever see. It's te reson goto has been removed from most modern languages. If you can't structure your code to make it terminate properly you should just quit coding. Jumping out of code like the way you request should normally only happen when a application is terminated and all memory is reclaimed by the system. Not exactly what you want is it?

So Yes create well defined blocks of code with a

do
    if not $gBreak then Function1()
    if not $gBreak then Function2()
    if not $gBreak then Function3()
Until $gBreak = 1

Having a loop with 500 lines in it.... Not strange you have to as such a question.

Link to comment
Share on other sites

well, that

do

if not $gBreak then Function1()

if not $gBreak then Function2()

if not $gBreak then Function3()

Until $gBreak = 1

is exactly what my main loop looks like

but every "Function1" in there will have their own loops and error checking mechanisms that will totally get stuck if the application I am working with terminates (imagine my script is filling an application's textbox1 with "userName" then my application just crashes, after my AdLibenable restarts the application, do you want to script to "press tab" and "Send("password")" and "starts clicking on screen checking emals" or start from the beginning to find where the progress is at first?) so the only solution is to add "if not $gBreak" in front of every single line of code?

Link to comment
Share on other sites

  • Moderators

$bBreak = True
AdlibEnable('_BreakLoop', 10)

While 1
    While $bBreak
        ;500 lines of code
    WEnd
    $bBreak = True
WEnd

Func _BreakLoop()
    $bBreak = Not $bBreak
EndFunc

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

thanks, but that will only break the while loop after it ran though 500 lines of code. my question is more like how to break the loop anywhere inside that 500lines of code: if the script is on line 345, I call _breakloop() then the rest of 155lines won't be executed at all.

Link to comment
Share on other sites

  • Moderators

thanks, but that will only break the while loop after it ran though 500 lines of code. my question is more like how to break the loop anywhere inside that 500lines of code: if the script is on line 345, I call _breakloop() then the rest of 155lines won't be executed at all.

Do you even bother to read or (OMG) try anything? Do you know how loops work? The loop will only work if the $bBreak is true, if not, it exits. I don't think you want AdlibEnable(). Is this message box that you want to restart your own?

$bBreak = True
;AdlibEnable('_BreakLoop', 10)

While 1
    While $bBreak
        ;500 lines of code
    WEnd
    $bBreak = True
WEnd

Func _One()
    While $bBreak
        ;whatever
    WEnd
    Return 1
EndFunc
Func _Two()
    While $bBreak
        ;whatever
    WEnd
    Return 1
EndFunc
Func _Three()
    While $bBreak
        ;whatever
    WEnd
    Return 1
EndFunc
;Func _BreakLoop()
    ;$bBreak = Not $bBreak
;EndFunc
Func _MsgBoxReset($nFlag, $sTitle, $sText, $nTime)
    Local $iMsg = MsgBox($nFlag, $sTitle, $sText, $nTime)
    $bBreak = Not $bBreak
    Return $iMsg
EndFunc

Next time, go with this philosophy, Put up or Shut up.

What that means is... Post some actual code so we don't have to play the guessing game.

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

  • Moderators

I do have an unothodox method to do what I want:

I can make 500lines of code into a separate exe file, then I can terminate it and restart it anytime I want.

That's not unorthodox IMHO. I use a similar method with /AutoIt3ExecuteScript when I write my update apps and want to restart after a download and install without the user having to do anything.

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

Yeah, I think I sort of understnd how loop works, it will either run the WHOLE thing inside the loop or it does not run ANYthing at all.

what if an interrupt happens right IN THE MIDDLE (or anywhere inside) of the loop and I don't want the loop to continue anymore?

AdlibEnable("_error_happened")

;=============================
;main script
_start_application()
while 1
    mousemove(1, 1)
    mousemove(1, 2)
    mousemove(1, 3)
    mousemove(1, 4)
    mousemove(1, 5)
    mousemove(1, 2)
    mousemove(1, 3)
    mousemove(1, 4)
    mousemove(1, 5)
    mousemove(1, 2)
    mousemove(1, 3)
    mousemove(1, 4)
    mousemove(1, 5)
    mousemove(1, 2)
    mousemove(1, 3)
    mousemove(1, 4)
    mousemove(1, 5)
    mousemove(1, 2)
    mousemove(1, 3)
    mousemove(1, 4)
    mousemove(1, 5)
WEnd
;=============================

func _error_happened()
    _start_application();restart the application
    goto mousemove(1, 1)
EndFunc

func _start_application()
    run excel.exe
EndFunc

so anyway to complete the above pseudo code without modifying the while loop?

Edited by moxinghbian
Link to comment
Share on other sites

I do have an unothodox method to do what I want:

I can make 500lines of code into a separate exe file, then I can terminate it and restart it anytime I want.

This is the first time you mention a seperate process. In that case just save the PID of the process and use ProcessClose in your Adlib function.

If you want to be unorthodox, thats your choice. Pleas put a warning in your posts so I know the next time. Then I don't have to waste my time.

Link to comment
Share on other sites

I am hoping for some other ways other than creating a new process.

and I think what I am asking is very simple, and it is pretty common if any scripts want to handle some exceptions, not sure why are ppl so frustrated about.

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