Problem: How To Enable a User to Interrupt a Closed Loop
Solution: Under normal circumstances, a For..Next loop or While..Wend loop cannot be interrupted until it has completed. This is known as a closed loop. Sometimes, however, it may be advantageous to enable a user to interrupt a closed loop in your application, allowing them to stop a potentially lengthy process.
It is in fact possible to interrupt a closed For..Next loop, using a heretofore undocumented method. Using this method, a pen tap can be used to interrupt the loop. No timer function is required.
The key is to check the pen status in your loop just before the Next statement. For example, if you add this to your loop just before the Next j:
For j ...
...
'check for pen tap to interrupt loop
if GetPenStatus(x,y) = true then Exit For
Next j
That enables the user to tap anywhere on the screen to interrupt the loop. The script then jumps to the statement immediately following the Next statement.
If you want to restrict the valid screen area that the user can tap on to interrupt the loop (for example on a Stop button), then further qualify your GetPenStatus check with the x/y pen coordinates. For example, let's say we have a STOP button on the screen with the rectangle bounds 0,120,160,160. You could just allow taps in that rectangle (actually just the y range 120..160 because the x ranges from 0..160) to stop the loop:
For j ...
...
'check for pen tap on Stop button to interrupt loop
if (GetPenStatus(x,y) = true) and ((y >= 120) and (y<=160)) then Exit For
Next j
This same technique can apply to While..Wend loops. This technique is applicable to both the PalmOS and PocketPC platforms.
Power Tip: Script Debugging Aid
The GetPenStatus function can also be used for script debugging as a pseudo breakpoint, for times when you want to pause a script without displaying a msgbox or using a delay statement. You can pause the script until the screen is tapped, and it then resumes from there.
An example would be:
'doing stuff before this, want to pause now
dim pX,pY
while GetPenStatus(pX,pY) = false
wend
'script resumes when the screen is tapped
The device will sit there in the endless While..Wend loop until you tap the screen.
Keywords: loop, interrupt, pause, GetPenStatus, debug, for, next, while, wend
KB ID: 10042
Updated: 2006-10-02
Satellite Forms KnowledgeBase Online