How can I interrupt a loop inside a Win32 C++Builder 6 application using, for instance, Escape?
I am sending commands from a TList through USB port using a for loop, which works fine and sometimes I need to interrupt the loop, but without using events once being inside the loop events like KeyDown or MouseDown are not active.
If your looking for a manual abort triggered by the user I don't know how else you can accomplish this except with an event. Just allow 1 key to abort like 'esc' and place it in a part of the for loop that isn't processing heavily so that it won't check for the event too often...
Otherwise, if the program can 'know' when to abort on its own then just test for that condition in the loop...Not sure what you're looking for (pun?)
Thanks Soranz,
I am really looking for a manual abort.
Basically the problem is how to detect or react to an event while processing another one.
Specifically the commando loop is inside a buttonClick event routine and the application is not able to start a procedure related to another event.
If there is a way to enable or detect an event inside the buttonClick procedure it could be a way. I am trying to avoid a multithread approach.
Maybe I'm wrong but shouldn't KeyDown or MouseDown work with what you're asking ? Did you disable keyboard/mouse events before the loop starts ? What kind of project are you making ?
Here's a quick n dirty way to trigger a user initiated break during a loop:
Soranz,
The application is a Windows application generated by the CBuilder 6. So it is based on events with a WinMain() entry point.
The loop is the one below:
void __fastcall TFormRobo::BitBtnEnviarListaClick(TObject *Sender)
{
char aux[100];
Screen->Cursor = crHourGlass;
for(int i=0; i < ListBox1->Items->Count; i++)
{
sprintf(aux,"%s\r",ListBox1->Items->Strings[i]);
SendString(aux);
}
Screen->Cursor = crDefault;
}
Where SendString is a rutine sending each char by the USB port.
While in the loop no other event is processed.
The question is how to interrupt it manually.
The main loop is:
WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
{
try
{
Application->Initialize();
Application->CreateForm(__classid(TFormRobo), &FormRobo);
Application->Run();
}
catch (Exception &exception)
{
Application->ShowException(&exception);
}
return 0;
}
Once started the application lies in the Application->Run() not accessible loop handling all events one by one, without re-entrancy.
The loop in your example is the main() loop of a not WIN32 GUI application.
I am looking for a way to detect something external like a Escape key. Remember that functions as kbhit(), getch(), getche() and others are not to be used with Win32 GUI applications.
If scanf is added at the begining of the for loop, no mater the key pressed the value of "c" is always the same, \0 or 'w' and 'w' key was never pressed.
Ahhh please format ur code like this because it's hard to read and comments with '\\'
The WINAPI is based on Events, Messages and Notifications. Having a loop like yours is there will hang the program like you said and doesn't make good use of this system. You have many options of which I'll mention one:
Perhaps, consider placing the loop in a Timer Event. Instead of your outer loop you can use the IDEvents from the timer to control when the next iteration of the loop begins. The system wont hang and you'll be able to add more 'usable' things to your program - like handling a WM_KEYDOWN message to abort or 'pause' the 'loop'.