Solved

Destroying form while in event handler

Posted on 2004-10-13
6
264 Views
Last Modified: 2010-04-05
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!
0
Comment
Question by:__alex
  • 3
  • 2
6 Comments
 
LVL 22

Accepted Solution

by:
Ferruccio Accalai earned 350 total points
ID: 12297427
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
 
LVL 14

Assisted Solution

by:DragonSlayer
DragonSlayer earned 150 total points
ID: 12297829
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
 
LVL 2

Author Comment

by:__alex
ID: 12297842
Aaargh... That simple. Thanks a lot.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 2

Author Comment

by:__alex
ID: 12297868
BTW: Closing a form is not destroying it ;-)
0
 
LVL 14

Expert Comment

by:DragonSlayer
ID: 12297882
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
 
LVL 2

Author Comment

by:__alex
ID: 12297946
Default Action is caHide (for non MDI childs)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Delphi XE2 application frozen on Windows 10 10 266
error 1.1 400 Bad request idhttp delphi 18 52
Printing problem 2 79
Dev Express grid collapse 2 39
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

932 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now