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!
LVL 2
__alexAsked:
Who is Participating?
 
Ferruccio AccalaiConnect With a Mentor Senior developer, analyst and customer assistance Commented:
why start another thread (and so run other memory) just to do this?

i'd simply add a check on the subform. An example to better explain:

unit Unit1; {main}

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

uses Unit2;

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
begin
form2.toclose:= True;
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
application.CreateForm(Tform2,Form2);
Form2.ToClose := False;
form2.Show;
end;

end.

unit Unit2; {subform}

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TForm2 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
    ToClose: Boolean; //a public check
  end;

var
  Form2: TForm2;

implementation

{$R *.dfm}

procedure TForm2.Button1Click(Sender: TObject);
var
i,x: Integer;

begin
for i := 1 to 10000 do begin //a sort of loop
caption := inttostr(i);
Application.ProcessMessages;
end;
showmessage(caption); // a domorestuff check
If ToClose then Close;
end;

end.
0
 
DragonSlayerConnect With a Mentor Commented:
a bit improved over F68's, so that you don't have to have a public ToClose variable in every such form that you want to send the close to (of course, you could always make a Form with ToClose and make all child forms inherit from it, but that's another story, heh):

unit Unit1;

const
  WM_CloseChild = WM_USER + 4096;

...

procedure TForm1.CloseForm2Button(Sender: TObject);
begin
  Form2.Perform(WM_CloseChild, 0, 0);
end;

and at form2:

unit Unit2;

const
  WM_CloseChild = WM_USER + 4096; // perhaps put this declaration in a global unit?

type
  TForm2 = class(TForm)
  private
    isClosing: Boolean;
  protected
    procedure WMCloseChild(var Msg: TMessage); message WM_CloseChild;
  end;

...

procedure TForm2.Form2Create(Sender: TObject);
begin
  isClosing := False;
end;

procedure TForm2.Button1Click(Sender: TObject);
var
  i, x: Integer;
begin
  for i := 1 to 10000 do begin //a sort of loop
    caption := inttostr(i);
    Application.ProcessMessages;
    if isClosing then Break;
  end;
  showmessage(caption); // a domorestuff check
  If isClosing then Close;
end;

0
 
__alexAuthor Commented:
Aaargh... That simple. Thanks a lot.
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
__alexAuthor Commented:
BTW: Closing a form is not destroying it ;-)
0
 
DragonSlayerCommented:
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
0
 
__alexAuthor Commented:
Default Action is caHide (for non MDI childs)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.