Board index » cppbuilder » PROBLEM: Exiting from for loop PLEASE HELP

PROBLEM: Exiting from for loop PLEASE HELP


2007-07-15 02:20:36 AM
cppbuilder23
hello
i have a problem with loop exit operation
there is a for loop in my software i need to terminate by SpeedButton
my loop looks like this (for example)
for(int i=0; i<2500; i++)
{
if(continue==false) break; //exit from the loop on request
//other RS232 operations
}
im seting the var to false state by clicking a SpeedButton
void __fastcall TForm1::SpeedButtonSTOPClick(TObject *Sender)
{
continue=false; //var is a global variable
}
but.... while a loop is running i can't change the "continue" variable.
my software is busy ... the SpeedButtonSTOPClick function is called just
after that (but it's too late)
how to solve this problem?
 
 

Re:PROBLEM: Exiting from for loop PLEASE HELP

When the button is clicked it generates a message that is sent to a window.
Therefore the the message system must be allowed to run for the program to
know about that click.
If you modify your loop in this way then messages can be processed and the
program will know about the button click.
for (int i = 0; i < 2500; ++i)
{
ProcessMessages();
if (continue == false)
break;
//other RS232 operations
}
. Ed
Quote
Marko wrote in message
news:46991400$ XXXX@XXXXX.COM ...

i have a problem with loop exit operation

there is a for loop in my software i need to terminate by SpeedButton

my loop looks like this (for example)

for(int i=0; i<2500; i++)
{

if(continue==false) break; //exit from the loop on request

//other RS232 operations

}

im seting the var to false state by clicking a SpeedButton

void __fastcall TForm1::SpeedButtonSTOPClick(TObject *Sender)
{
continue=false; //var is a global variable
}


but.... while a loop is running i can't change the "continue" variable.
my software is busy ... the SpeedButtonSTOPClick function is called just
after that (but it's too late)


how to solve this problem?
 

Re:PROBLEM: Exiting from for loop PLEASE HELP

Hi ??
for(int i=0; i<2500; i++)
{
Application->ProcessMessages(); //Makes the app check for messages
if(continue==false) break; //exit from the loop on request
//other RS232 operations
}
I asume You are checking RS232 ports and if it was me
I would use a TTimer Event instead of the for loop.
That way You can also deside how often the ports should
be checked + Your kill button will have effect + Your
app will not take all the CPU pover, in case that
the user wants to do something else while waiting for
respons.
If this is for the mechanical world and You are using
some kind of serial port I doesn't make much sence to
check that often anyway, if I remember correctly the
responce time is about 100 milli seconds, which would
then be a good value for the TTimer->Interval.
Kind regards
Asger
 

{smallsort}

Re:PROBLEM: Exiting from for loop PLEASE HELP

Marko < XXXX@XXXXX.COM >wrote:
Quote
if(continue==false) break; //exit from the loop on request
I'm deeply amused by this. Why? Because 'continue' is a C and C++
keyword, whose meaning is pretty much that of 'not break'.
Alan Bellingham
--
Team Browns
<url:www.borland.com/newsgroups/>Borland newsgroup descriptions
<url:www.borland.com/newsgroups/netiquette.html>netiquette
 

Re:PROBLEM: Exiting from for loop PLEASE HELP

yes!!! but it was just an example.... i'm using another variable name ....
i found a solution: Application->ProcessMessages() works very good
thanks for all ;)
Alan Bellingham pisze:
Quote
Marko < XXXX@XXXXX.COM >wrote:

>if(continue==false) break; //exit from the loop on request

I'm deeply amused by this. Why? Because 'continue' is a C and C++
keyword, whose meaning is pretty much that of 'not break'.

Alan Bellingham
 

Re:PROBLEM: Exiting from for loop PLEASE HELP

Marko < XXXX@XXXXX.COM >wrote:
Quote
yes!!! but it was just an example.... i'm using another variable name ....


i found a solution: Application->ProcessMessages() works very good
Wait until one of your users has hit the "start again"
button while processing messages from within your loop.
Quote
[...]
--
XXXX@XXXXX.COM is never read
I'm HSchober at gmx dot de
"If there were some arcane way to remove the heads of every
newsgroup troll on the planet, I think it would elevate
humans to a whole new level of intelligence."
Rocky Frisco
 

Re:PROBLEM: Exiting from for loop PLEASE HELP

Marko < XXXX@XXXXX.COM >writes:
Quote
yes!!! but it was just an example.... i'm using another variable name ....


i found a solution: Application->ProcessMessages() works very good
That's one of those "partial solutions" that is seductive. The
problem is, you have one event loop that is dispatching to a handler,
which in turn runs another event loop.
What if that loop dispatches to the same event handler, which runs yet
another event loop, which runs another event loop, which runs....
... until you overflow your stack. This may not be an issue in simple
programs, or situations where the work is very small. But since you
say you have an pseudo-infinite loop that only breaks upon some
specific externally injected condition, it seems entirely possible to
get multiple loops running. Sure, one can code around these issues,
but it does add complexity to the process, and at the very least, it's
useful to consider what really can happen in a program.
(I've experienced this exact problem with a high-volume corba
application, where it was waiting for a remote response, and ran the
event loop in the meanwhile, and another message came in causing
another remote call, which ran another event loop, and so on. When
messages come in too fast, it crashes, without appropriate measures to
counteract this behavior.)
--
Chris (TeamB);