Page 1 of 1

PostPosted: Thu Jan 09, 2003 9:54 pm
by Main Chen

How to break for() use a button ?
I try the source, It can't run,
Thank you!


bool StopFind;

handler FindListForm.onopen() {
StopFind = false;

handler FindListForm.ondraw() {
int i;
StringList List;

for(i=0; i<10000; i++) {
if(StopFind) break;

handler StopFindBtn.onselect() {
StopFind = true;

PostPosted: Thu Jan 09, 2003 11:13 pm
by jfield
Running a for loop and/or running the Form.ondraw method may be higher priority than, or just have to finish before, the Button.onselect method.

In your code i is an integer, but the StringList.add method expects a string.

PostPosted: Thu Jan 09, 2003 11:41 pm
by samps
In your code i is an integer, but the StringList.add method expects a string."

Doesn't matter.
One of the greatest strengths of Orbforms D and PocketC is the automatic typecasting. (And the addition of the string type to C of course :-)


PostPosted: Fri Jan 10, 2003 4:17 am
by cao
Hi Main Chen:

ondraw handler is called when the form wants to draw the items on the screen. It is not designed to do the main algorithm or find operation. You should do the computation work somewhere else.

If you felt the computation can take a long time, you need to look into use a timer or event, and break up the length computation.

This is just a suggestion, i use this approach to implement a stop button in a length computation on the device.

For example:

bool bPause = false; /* define this variable somewhere */

handler btnFindStart.onselect() {
bPause = false;

handler btnFindStop.onselect() {
bPause = true;

mainForm.ontimer() {

/* do a small chunk of find operation. For example: just search a N records */
/* if no pause, continue the timer to cause the find operation to search next N records. */
if (false==bPause) { timer(10); }

let me know if this approach works for you.

PostPosted: Fri Jan 10, 2003 5:57 am
by samps
It doesn't work to stop a FOR-loop tho'

Try this:

int i, stopped;

for (i=1;i<1000;i++)
if (stopped!=1)


btnStart starts the show,Label shows i's from 1 to 999 no matter how many times you click btnStop.

Maybe there's a need for something like the Delphinian Application.ProcessMesages, to make sure that applications keep an eye out for button clicks and the like, even while they're busy doing other stuff.


PostPosted: Fri Jan 10, 2003 4:34 pm
by cao

Your example doesnot capture my suggestions.

It does not use ontimer() mechanism. Compare your code and my example, I don't see anywhere you used ontimer.
I suggested to break up the lengthy loop to smaller one. Your example still have this one long loop 1->10000

for (i->10000) can be break up into smaller steps.

It gives the UI a chance to accept user input.

PostPosted: Mon Jan 13, 2003 4:35 am
by Main Chen
<blockquote id="quote"><font size="1" face="Verdana, Arial, Helvetica" id="quote">quote:<hr height="1" noshade id="quote">
let me know if this approach works for you.
<hr height="1" noshade id="quote"></blockquote id="quote"></font id="quote">

Hi Cao

I try the code, it's Ok!
Thank you very much.

PostPosted: Mon Jan 13, 2003 8:23 pm
by dewey
Event handlers are called one at a time - if you put a long computation into one handler, no other handler is able to run until the previous handler completes. This is why Kevin's suggestion of using the timer event to process a small chunk of algorithm is so appropriate.