Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 122823
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T04:11:22+00:00 2026-05-11T04:11:22+00:00

I have a MDI application written in Delphi 2007. If the user exits a

  • 0

I have a MDI application written in Delphi 2007.

If the user exits a form within it whilst code is executing it causes an exception, because the code is trying to update a component or use an object that has been freed with the form.

Is there anyway I can tell if code is executing in the exit event or is there a standard way to deal with this situation?

Update with more infomation

The exception usually happen in the following circumstance.

A button on the child mdi form is pressed, this activates a function in the form, the function will go to the database and retrieve data, it will then re-format it and display it in a visual component on the form (usable a TListView).

If the code is taking a long time to execute (say if there is a lot of data to process) the user will lose interest and click the close button (the speed of the code is been worked on to try to avoid this).

The code inside the function is still executing even though the form it belongs to has been freed (The code is in the private section of the form), now when it trys to update the visual components they no longer exist (as they were freed with the form) and it throws a exception.

The code in the child form is usably in a loop when this happen, cycling records and update the listview accordingly, the loops contain code that looks like so

inc(i); if (i mod 25) = 0 then begin     StatusPnl.Caption := 'Loading ' + intToStr(i) + ', Please wait';     application.ProcessMessages; end; 

Other Code samples

the fromClose event looks like so

//Snip if (Not (Owner = nil)) then with (Owner as  IMainForm)do begin     //Snip     DoFormFree(Self,Self.Name); end else //Snip 

DoFormFree is a function in the main mdi parent form and looks like so

//Snip (G_FormList.Objects[x] as TBaseForm).Release; G_FormList.Objects[i] := nil; G_FormList.Delete(i); //Snip 

All forms are stored in a list, as for various reasons, and all child forms extend the TBaseForm class.

Ideally I would like a way to tell if code in a form is executing, and prevent the user from closing the form, or hide it until the code is finished, as in some instances it may be generating a report and update as status panel when the exception happen, in that case the report will be incomplete.

as all forms are sub classes of TbaseFrom some global way of doing this would be ideal, so I can add the code to the base form and have it work on all descended forms.

  • 1 1 Answer
  • 3 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T04:11:23+00:00Added an answer on May 11, 2026 at 4:11 am

    You provide not enough information, but the easiest solution that comes to mind is to test in the OnCloseQuery handler whether code is executing, and if so set CanClose to False.

    Alternatively you can decouple the code from the MDI form, by creating an intermediate object that both the form and the background code know about. You let this have a reference to the form, which is reset when the form is closed. By routing all access to the form through this intermediate object you can prevent the exceptions.

    Edit: You need to provide information on how you execute the code that tries to access the MDI form after it has been freed. There are some ways to execute worker code, like:

    • in a method of the form or of another object
    • in a OnTimer event handler
    • in the OnIdle handler of the Application object
    • in a background thread

    Note that in the first case the form could only be freed if you either do it yourself in code, or if you call Application.ProcessMessages. Without more information about what your code looks like, nobody can give you a specific answer to your question.

    Edit 2: With your added information it seems that the code in question is always executed in methods of the form. This is easy to catch by creating a boolean member that is set to True when the execution starts, and that is set to False when the execution has finished. Now you only need to add a handler for OnCloseQuery in your base class, and set CanClose to False if the member (fExecuting for example) is True. You can silently forbid closing, or show an information box. I’d simply show a progress form or display something in the status bar, so as not to interrupt the user too much with modal info boxes.

    What I would definitely do is allowing the user to cancel the long running process. So you could also show a message box asking the user whether they want to cancel the operation and close. You still need to skip the closing of the form then, but can store the request to close, and process it once the execution has ended.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a MDI parent form. When user presses Enter I want the Application
I have a .net MDI application written in vb.net. I'm trying to program a
I have written an MDI based application, in which the child forms are of
I have MDI application and opeing new form on focus/selection. To avoide opening of
I have a MDI Windows Forms application. The main form, among the other events,
I have an mdi child form as a dockable content in my application and
My application looks like this: I have an MDI parent form - form1, the
I have an MDI application. When I show a message box using MessageBox.Show(), the
Hi I have an MDI application where a child window handles a number of
Using C# and the .Net framework 2.0. I have an MDI application and need

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.