Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Aborting time demanding codes/operations?

Status
Not open for further replies.

801119

Programmer
Apr 10, 2000
311
SE
If a time demanding operation is under way and I want to enable the user to abort it by pressing a key, for example ‘Escape’, how is that possible?
For example, the code adds 8000 strings to a TListBox

void __fastcall TForm1::Button1Click(TObject *Sender)
{
TListBox->Items->Clear();
int x=8000;
while(--x>=0) // time demanding operation here
TListBox->Items->Add(x);
}

As we all know, this kind of code “disables” the form, in this case Form1, for a few seconds depending on the system, until code has finished operation.
How can one write a similar code that doesn’t “disable” the form, and enables the user to abort it by pressing another button?

All I want is a few hints on how it ”could” be done, or if anyone has a code that actually works something like this I should be pleased if he/she would let he have a peek at it! =)

Martin G Broman
mgb_svea@thevortex.com

DWS - Alpha Whitin Dead Wolf Society
 
That while loop syntax. Does that actually work? Placing the decrement (--) operater in the conditional statement like that?
This is untested, but have you tried setting the KeyPreview property to true in the Obeject Inspector? From what I hear, this lets the form recieve keyboard input first before any of the other controls that has focus on the form.
 
Yes it does work, though it sounds a bit crazed-up!!
I tried to set KeyPreview to 'true', but it didn’t work as I had planned.
Maybe I shouldn’t have written that while loop! (?)
The program I am writing will search a folder (inc sub dirs) and save to a file!
It takes a minute or two to search the entire HDD, here is it were I want to enable the user to abort the scan! There is a problem. As soon as the user starts the scan of HDD, (or any other dir) the program locks the user out (or something like that), the user cant do anything while the program scans the HDD! I tried to change the program to look something like this:

bool usr_abort==false;
void __fastcall TForm1::Button1Click(TObject *Sender)
{
TListBox->Items->Clear();
int x=8000;
while(--x>=0 && usr_abort==false) // time demanding operation here
TListBox->Items->Add(x);
}
void __fastcall TForm1::Button2Click(TObject *Sender)
{
usr_abort = true;
}

This is how I figured it to be done! Didn’t work!! Maybe I didn’t quite understand you! (?)
If you got any further suggestions, I will welcome all!!

Martin G Broman
mgb_svea@thevortex.com

DWS - Alpha Whitin Dead Wolf Society
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top