I have a modal form that displays the progress of a lengthy operation. The operation is triggered when the Form's OnActivate Event is triggered.
procedure TMyForm.FormActivate(Sender:TObject);
begin
Start;
end;
The form has a cancel button with the ModalResult property set to mrCancel and the OnClick handler sets a flag that causes the operation to end.
procedure TMyForm.CancelButtonClick(Sender: TObject);
begin
FCancel := True;
end;
When I click the cancel button it stops the operation as expected but it fails to close the form. I suspect this is because the OnActivate handler is blocking the form from closing. A second click of the button does close the form. I've tried calling Close and sending a wm_close message but nothing seems to work. Does anyone have any suggestions to get the form to respond on the first click? Perhaps another event I can use instead of OnActivate?
I know moving the operation to a thread will be suggested. That's not a possibility at this point due to a large amount of poorly written legacy code.
if not FCancel then ...
– Teun Pronk