Solved

Destroying form while in event handler

Posted on 2004-10-13
6
272 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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
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

ScreenConnect 6.0 Free Trial

Want empowering updates? You're in the right place! Discover new features in ScreenConnect 6.0, based on partner feedback, to keep you business operating smoothly and optimally (the way it should be). Explore all of the extras and enhancements for yourself!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Newbie Thread Programming 1 151
How to debug For loops? 3 49
Is anyone willing to have a look through this code and help debug? 25 74
PHP preg_replace code convert to Delphi 14 55
A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.

770 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