Solved

Destroying form while in event handler

Posted on 2004-10-13
6
260 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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

760 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

18 Experts available now in Live!

Get 1:1 Help Now