I have a MDI application written in Delphi 2007.
If the user exits a form within it whilst code is executing it causes an exception, because the code is trying to update a component or use an object that has been freed with the form.
Is there anyway I can tell if code is executing in the exit event or is there a standard way to deal with this situation?
Update with more infomation
The exception usually happen in the following circumstance.
A button on the child mdi form is pressed, this activates a function in the form, the function will go to the database and retrieve data, it will then re-format it and display it in a visual component on the form (usable a TListView).
If the code is taking a long time to execute (say if there is a lot of data to process) the user will lose interest and click the close button (the speed of the code is been worked on to try to avoid this).
The code inside the function is still executing even though the form it belongs to has been freed (The code is in the private section of the form), now when it trys to update the visual components they no longer exist (as they were freed with the form) and it throws a exception.
The code in the child form is usably in a loop when this happen, cycling records and update the listview accordingly, the loops contain code that looks like so
inc(i);
if (i mod 25) = 0 then
begin
StatusPnl.Caption := 'Loading ' + intToStr(i) + ', Please wait';
application.ProcessMessages;
end;
Other Code samples
the fromClose event looks like so
//Snip
if (Not (Owner = nil)) then
with (Owner as IMainForm)do
begin
//Snip
DoFormFree(Self,Self.Name);
end
else
//Snip
DoFormFree is a function in the main mdi parent form and looks like so
//Snip
(G_FormList.Objects[x] as TBaseForm).Release;
G_FormList.Objects[i] := nil;
G_FormList.Delete(i);
//Snip
All forms are stored in a list, as for various reasons, and all child forms extend the TBaseForm class.
Ideally I would like a way to tell if code in a form is executing, and prevent the user from closing the form, or hide it until the code is finished, as in some instances it may be generating a report and update as status panel when the exception happen, in that case the report will be incomplete.
as all forms are sub classes of TbaseFrom some global way of doing this would be ideal, so I can add the code to the base form and have it work on all descended forms.