?
Solved

Destroying form while in event handler

Posted on 2004-10-13
6
Medium Priority
?
290 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 23

Accepted Solution

by:
Ferruccio Accalai earned 1400 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 600 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Want to be a Web Developer? Get Certified Today!

Enroll in the Certified Web Development Professional course package to learn HTML, Javascript, and PHP. Build a solid foundation to work toward your dream job!

Question has a verified solution.

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

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…
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…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses

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