I tried to find answer, but I didn't find it.
I want to do something like this: when I click button, I would like it开发者_如何学JAVA to repeat actions till another button is clicked
I have something like this:
void __fastcall TForm1::BitBtn1Click(TObject *Sender)
{
if(pole!=NULL){
pole->przesun_lidera(pole->wladca);
pole->rozstaw();
pole->rysuj_plansze();
}
}
and I want to repeat actions within "if", until I click another button.
Could someone tell me, how can I achieve this?
I think you have two possibilities here. One is to make a thread and execute your code in it until a condition, set by another button is set. Another possibility is to allow the message pump to process messages while inside the loop, by calling ProcessMessages(), e.g.
void __fastcall TForm1::BitBtn1Click(TObject *Sender)
{
condition = false;
while( !condition && pole!=NULL){
pole->przesun_lidera(pole->wladca);
pole->rozstaw();
pole->rysuj_plansze();
Application->ProcessMessages();
Sleep(1); // don't be a processor hog
}
}
void __fastcall TForm1::BitBtn2Click(TObject *Sender)
{
condition = true;
}
You will need to have a function that is executed in the "background".
Windows is an event driven system. Your functions are only activated when Windows receives an event. My understanding is that you want something to happen while waiting for a specific event (button press) to occur. This time between events is "background" time.
One idea is to have Windows execute your function while it is waiting. Search the web for "Windows spin wait". This will give you information on how to "hook" a function to the background or spin-wait loop.
You may also want to create another thread as a background task. Have your first button click turn on the background thread. The background thread will execute until a semaphore or wait-object is set. The second button press will set this semaphore / wait-object, informing the background task to stop. There are similar methods, but this is the foundation of the issue.
精彩评论