[futurebasic] Re: Break ... Until

Message: < previous - next > : Reply : Subscribe : Cleanse
Home   : December 1997 : Group Archive : Group : All Groups

From: Rick Brown <rbrown@...>
Date: Fri, 26 Dec 1997 16:02:56 -0600
Peter wrote:

> Dear Therapist
> 
> For years I appear to have been suffering from the delusion that I knew how
> to program in BASIC.

Ah, a common delusion.  Have a seat on the couch here.

> I've been trying to debug this code:
> _____________________________________
> GLOBALS
> DIM x
> DIM programEnds
> END GLOBALS
> x=0
> programEnds=0
> 
> LOCAL FN beeper
>   DO
>     BEEP
>   UNTIL x=1
> END FN
> 
> LOCAL FN handleBreak
>   x=1
>   programEnds=1
> END FN
> 
> LOCAL FN doTimer
>   FN beeper
> END FN
> 
> ON BREAK FN handleBreak
> ON TIMER(2) FN doTimer
> DO
>   HANDLEEVENTS
> UNTIL programEnds
> _____________________________________
> 
> Debug? Debug? Whats to debug? Should just run as is!
> 
> However ... what happens is that the variables assigned in handleBreak are
> never picked up in the Fn beeper Do... Until structure, or in the
> Handleevents, and the Fn beeper keeps going forever, so I must force a quit
> out of FB2 instead of just being able to do a command period.

Your delusion has its roots in a common misconception that many of my
patients suffer--namely, that if you declare a BREAK handler with the ON
BREAK FN statement, then a subsequent keying of Command-period will
cause your program to jump to the defined FN.  Hah!

It doesn't.  What the keying of Command-period does is to store what you
might call a "Command-period event" into the event queue.  That's all. 
_Then_, the next execution of HANDLEEVENTS reads the queue, finds the
Command-period event, and actually effects the jump to your ON BREAK
function.

So let us psychoanalyze your program.  For the first 2 seconds, it
continuously rolls through your "DO: HANDLEEVENTS: UNTIL programEnds"
loop.  After 2 seconds have elapsed, it jumps to your FN doTimer, which
sends it immediately to FN beeper.

Now FN beeper executes a loop and keeps beeping like crazy.  You sit
there hammering away at Command-period, and this causes Command-period
events to stack up in the queue.  But since no HANDLEEVENTS is being
executed, the queue never gets read, FB doesn't know it's supposed to
jump to FN handleBreak, x never gets set to 1, and the beeping
continues.

The way to resolve the issue in _this_ code is to put a HANDLEEVENTS in
the loop in FN beeper (or, if multiple HANDLEEVENTS makes you
uncomfortable, you can alternatively put these two lines in the FN
beeper loop:
   BREAK ON
   BREAK OFF
Those lines act like a HANDLEEVENTS which is limited to handling
Command-period events.)  However, you've stated that your "real" code is
intended to do something different, so how you resolve it will depend on
what your real code is supposed to do.