Board index » cppbuilder » breaking a for loop

breaking a for loop


2006-05-07 03:19:30 AM
cppbuilder103
I have a for loop that runs for several minutes and I am having trouble
activating a button to stop the loop. The button OnClick event does not
process while the loop is running. I have added
Application->ProcessMessages(); throughout the for loop to try and catch the
mouse click but that is not working. How can I force an event to process
during a for loop? Do I need to multithread the stop button?
Thanks
Larry
 
 

Re:breaking a for loop

LarryJ wrote:
Quote
... I have added
Application->ProcessMessages(); throughout the for loop to try and catch the
mouse click but that is not working.
Well that should work. But what code do you have in the buttoneventhandler ?
Just take a privat variable
bool userstop = false;
And set it to true in the eventhandler. In your for loop place
statements like:
for ( .....)
{
.....
Applcation->ProcessMessages();
if ( userstop ) break;
}
Hans.
 

Re:breaking a for loop

That is exactly how I setup the "stop" button OnClick event. The problem is
that the "stop" button OnClick event will not run when the for loop is
running so the Applcation->ProcessMessages(); does not have anything to
process. Do you have any idea how I make that "stop" button process?
"Hans Galema" < XXXX@XXXXX.COM >wrote in message
Quote
LarryJ wrote:

Hans.
 

{smallsort}

Re:breaking a for loop

"LarryJ" < XXXX@XXXXX.COM >wrote in message
Quote
That is exactly how I setup the "stop" button OnClick event. The
problem is that the "stop" button OnClick event will not run when
the for loop is running so the Applcation->ProcessMessages(); does
not have anything to process. Do you have any idea how I make that
"stop" button process?
ProcessMessages() will handle your button click, but not until your
loop actually calls it. Are you doing some lengthy processing between
calls?
--
Bruce
 

Re:breaking a for loop

I have a counter that increases each time the ProcessMessages() is called.
This is called about every 10 seconds. The problem is that when the for loop
is running in the function NewEndDate( Sender ); no button can be clicked
and I don't understand why. While the for loop in NewEndDate( Sender ); is
running I need to click Button14 to stop the for loop.
void __fastcall TDownLoadForm::Button13Click(TObject *Sender)
{
if( DownLoadForm->RunningUpdate==0 ){ //Not Running So Start
DownLoadForm->RunningUpdate=1;
Button13->Caption="Stop";
DownLoadForm->StopUpdate=0;
NewEndDate( Sender ); //This contains a for loop where
Application->ProcessMessages(); is called
}else if( DownLoadForm->RunningUpdate==1 ){
DownLoadForm->RunningUpdate=0;
DownLoadForm->StopUpdate=0;
Button13->Caption="Lookup New End Date and Record Count for Symbol
Lists";
}
}
void __fastcall TDownLoadForm::Button14Click(TObject *Sender)
{
DownLoadForm->StopUpdate=1;
}
//---------------------------------------------------------------------------
" Bruce Salzman" < XXXX@XXXXX.COM >wrote in message
Quote

ProcessMessages() will handle your button click, but not until your loop
actually calls it. Are you doing some lengthy processing between calls?

--
Bruce

 

Re:breaking a for loop

Application->ProcessMessage() will never be called until the loop is
finished... you must put this call inside the loop, not on your OnClick
event, the same way Hans showed to you...
Application->ProcessMessages() will get the messages from the queue e
process them, so when you do this:
for (...)
{
...
}
Application->ProcessMessages();
The messaging processing will take place AFTER the loop is complete, isn't
it?
[]s
Fred
"LarryJ" < XXXX@XXXXX.COM >wrote in message
Quote
I have a counter that increases each time the ProcessMessages() is called.
This is called about every 10 seconds. The problem is that when the for
loop is running in the function NewEndDate( Sender ); no button can be
clicked and I don't understand why. While the for loop in NewEndDate(
Sender ); is running I need to click Button14 to stop the for loop.


void __fastcall TDownLoadForm::Button13Click(TObject *Sender)
{
if( DownLoadForm->RunningUpdate==0 ){ //Not Running So Start
DownLoadForm->RunningUpdate=1;
Button13->Caption="Stop";
DownLoadForm->StopUpdate=0;
NewEndDate( Sender ); //This contains a for loop where
Application->ProcessMessages(); is called
}else if( DownLoadForm->RunningUpdate==1 ){
DownLoadForm->RunningUpdate=0;
DownLoadForm->StopUpdate=0;
Button13->Caption="Lookup New End Date and Record Count for Symbol
Lists";
}
}


void __fastcall TDownLoadForm::Button14Click(TObject *Sender)
{
DownLoadForm->StopUpdate=1;
}
//---------------------------------------------------------------------------



" Bruce Salzman" < XXXX@XXXXX.COM >wrote in message
news:445d54c1$ XXXX@XXXXX.COM ...
>
>ProcessMessages() will handle your button click, but not until your loop
>actually calls it. Are you doing some lengthy processing between calls?
>
>--
>Bruce
>


 

Re:breaking a for loop

Uh... sorry.... I read Application->ProcessMessages() after "NewEditDate()"
and assumed that is actually a call.... post your loop too... then we can
help you...
[]s
Fred
"LarryJ" < XXXX@XXXXX.COM >wrote in message
Quote
I have a counter that increases each time the ProcessMessages() is called.
This is called about every 10 seconds. The problem is that when the for
loop is running in the function NewEndDate( Sender ); no button can be
clicked and I don't understand why. While the for loop in NewEndDate(
Sender ); is running I need to click Button14 to stop the for loop.


void __fastcall TDownLoadForm::Button13Click(TObject *Sender)
{
if( DownLoadForm->RunningUpdate==0 ){ //Not Running So Start
DownLoadForm->RunningUpdate=1;
Button13->Caption="Stop";
DownLoadForm->StopUpdate=0;
NewEndDate( Sender ); //This contains a for loop where
Application->ProcessMessages(); is called
}else if( DownLoadForm->RunningUpdate==1 ){
DownLoadForm->RunningUpdate=0;
DownLoadForm->StopUpdate=0;
Button13->Caption="Lookup New End Date and Record Count for Symbol
Lists";
}
}


void __fastcall TDownLoadForm::Button14Click(TObject *Sender)
{
DownLoadForm->StopUpdate=1;
}
//---------------------------------------------------------------------------



" Bruce Salzman" < XXXX@XXXXX.COM >wrote in message
news:445d54c1$ XXXX@XXXXX.COM ...
>
>ProcessMessages() will handle your button click, but not until your loop
>actually calls it. Are you doing some lengthy processing between calls?
>
>--
>Bruce
>


 

Re:breaking a for loop

LarryJ wrote:
Quote
I have a counter that increases each time the ProcessMessages() is called.
This is called about every 10 seconds. The problem is that when the for loop
is running in the function NewEndDate( Sender ); no button can be clicked
and I don't understand why. While the for loop in NewEndDate( Sender ); is
running I need to click Button14 to stop the for loop.
I think you will find that Button13 cannot receive/process another
message until it finishes processing the current message.
Button13Click should start a thread to run the download, then return.
Then, it can receive the "stop" click, and can even terminate the
thread if you don't want to wait for it to get around to checking your
flag.
 

Re:breaking a for loop

If you'd post your code for NewEndDate() then we might see better what the
problem is. I created a test app to reproduce what I think your code's flow
should look like. Here is my code:
bool runningUpdate = false;
bool stopUpdate = false;
void LoopIt()
{ // Will sleep for 10 seconds unless the button is clicked
for (int index = 0; index < 100 && !stopUpdate; ++index)
{
Sleep(100);
Application->ProcessMessages();
}
}
void __fastcall TForm1::Button1Click(TObject *Sender)
{
if (!runningUpdate)
{
runningUpdate = true;
Button1->Caption = "Stop";
stopUpdate = false;
LoopIt();
stopUpdate = false;
runningUpdate = false;
Button1->Caption = "Go";
}
else
{
stopUpdate = true;
}
}
- Clayton