Frequently Asked Questions
How can I abort a lengthy operation?
Question:
How can I abort a lengthy operation?
Answer:
Create a global boolean variable to indicate if a abort
button has been pressed. Call Application.ProcessMessages
to allow other events (the changing of the abort flag) in
your application to process. Check the value of the global
boolean during your lengthy event, and break out if it
returns true. Calling Application.ProcessMessages will also
allow other applications to process, allowing for a smooth
flow of system events. Note that you must be careful to
control re-enterance, since other controls in your
application may receive messages as well.
Example:
var bAbort : bool;
procedure TForm1.Button1Click(Sender: TObject);
{Start the loop}
var
i : integer;
j : integer;
begin
bAbort := false;
j := 0;
for i := 1 to 1000000 do begin
j := j + 1;
Application.ProcessMessages;
if bAbort = true then break;
end;
if bAbort = true then
ShowMessage('Operation Aborted') else
ShowMessage('Operation Successful');
end;
procedure TForm1.Button2Click(Sender: TObject);
{Abort the operation}
begin
bAbort := true;
end;