Link to home
Start Free TrialLog in
Avatar of __alex
__alex

asked on

Destroying form while in event handler

I've got a main form and a subform. My main form is responsible for creating and destroying the subform. In an event handler (a ButtonClick) of the subform there's a loop like

for i := 0 to whatever do
  DoStuff;
  Application.ProcessMessages;   // To refresh some controls
  DoMoreStuff;   //  (*)
end;

I want the user to be able to destroy the subfrom via main form while the subform is in that loop. After the destructor has finished the program continues at (*) which leads to a crash (because the resources accessed in DoMoreStuff are gone).
I thought about not to close the subform in the main form but start another thread that monitors the loop and closes the subform from this thread but that's breaking a fly on the wheel...
Any hints? Thanks in advance!
ASKER CERTIFIED SOLUTION
Avatar of Ferruccio Accalai
Ferruccio Accalai
Flag of Italy image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of __alex
__alex

ASKER

Aaargh... That simple. Thanks a lot.
Avatar of __alex

ASKER

BTW: Closing a form is not destroying it ;-)
Closing a form will lead to it being destroyed, unless you set Action to caNone at the OnClose =p

you can also optionally, call Release, when you receive the message
Avatar of __alex

ASKER

Default Action is caHide (for non MDI childs)