Use Alert to break out of a loop in a routine

Good morning,

I am experimenting with Alerts, studied the documentation and the posts that I find on ClarionHub. I’m using CW6.3 ABC. I’ve been trying things but still missing something.

Attached is an image I hope is visible.

Nothing happens when I press the Esc key nor does the test message at the Window Event AlertKey get triggered. I’m trying to provide an Esc to break out of a Routine. See the code snippet in the image.

This isn’t critical to me but rather something I would like to be able to implement as it may be useful in my multi-dll veterinary app. Learning something new is always good.

Thank you for any guidance. Regards to all.
Doug Selzler

You are in a “tight loop”, there’s no chance for your code to receive an event.
To be responsive to events, you need to let the program flow get back to the ACCEPT statement.

So you’ll need to break your process into parts where you intialize everything and then do a small amount of work

  • maybe just one time through the loop,
  • or pay attention to clock
    • and break out of the loop when you’ve hit a fixed amount of time
    • for example 0.1 seconds

Once you’ve broken out of the loop, if there is more work to be done then POST(EVENT:User). When you receive EVENT:User you do some more work
This gives your user the chance to press ESC and the code will have a chance to see that and respond.

I dimly recall that there is a process template which helps you do all of this
Sorry I’m an hand coder - I don’t use templates so it’s hard to remember what they do :wink:

Not sure how well this works in a windows app, but you can use KEYBOARD() loop_iteration_structure_.htm [Clarion Community Help]

example from the help page:

  LOOP WHILE KEYBOARD()         !Empty the keyboard buffer

    ASK                          !without processing keystrokes

  UNTIL KEYCODE() = EscKey      !but break the loop for Escape

Personally, I would stay away from that kind of tight loop stuff.

Try to set the window timer.
On the window event: timer check the KEYCODE
IF KEYCODE() = EscKey

END

Mark, Jeff & Rudy,

Thank you.

I played with each of the suggestions and have decided “tight loops” are best avoided. The software has been around for decades and some long time users have accumulated hundreds of thousands of records over that time. When running reports or analysis with large date ranges, I have presented a warning message to the user advising they use a shorter interval .

But I have learned, over many years, that no matter how clearly I may word a message and offer alternatives that messages are seldomly read. Well too bad I guess. I don’t generally advise “ending the task” but some users know that is available.

Always trying to make the software friendlier. Still having fun. Thanks again for a wonderful resource.

– Doug

One thing you can do is have smaller tight loops, inside an ACCEPT loop, on the timer event.

So process x number of records, then process more on the next timer loop, and optionally update your progress bar.

That will give your window’s Cancel button an opportunity to get seen, while having acceptably tiny tight loops.

yes use the process template and include a cancel button.

that way it is all done for you…

I looked in my progress class, which I use in places where I have a tight loop.
I use the KEYBOARD ASK approach. .
If I were to pull out the code and put it in a loop, it would look like this.

 abort=false
 LOOP	
            If KEYBOARD()
                Ask()
                If Inlist(KEYCODE(),PauseKey,SpaceKey,EscKey)
                    If Message('Do you want abort process?','???',Icon:Hand,Button:Yes+Button:NO)=Button:Yes
                        abort=true
                    END					
                END
            END
            if abort
                BREAK
            END
   !stuff to do
  End