Is there a Delphi event that is guaranteed to be called when an app terminates but before any forms are destroyed?
Asked Answered
H

2

8

I am using Delphi 6 Pro. I need an event that will fire when my application is definitely shutting down but before any Forms are destroyed. I thought about trapping WM_QUIT messages in the main form WndProc() but that didn't work because the Forms started destroying before I saw a WM_QUIT message. I was wondering if there is a standard Delphi event that I can use to execute code at this point of the shutdown chain of events? I can't use the main form's FormClose() event since it isn't guaranteed to fire, and the FormDestroy() event is too late. Any ideas?

Hampshire answered 10/10, 2011 at 23:28 Comment(0)
B
14

You will not see a WM_QUIT messsage arrive in the WndProc() method, as WM_QUIT is a signal for message loops to stop running, and as such it is not usually dispatched to a window procedure.

There is no specific event for what you are asking. What you can do, however, is open your project's .dpr file and put whatever code you need after the call to Application.Run exits. The message loop is no longer running, but the Application and MainForm objects have not been freed yet.

Beery answered 11/10, 2011 at 0:4 Comment(2)
Excellent @Remy Lebeau. I didn't think of using the project file.Hampshire
The only problem with this is that after Application.Run has finished everything has effectively closed down and only the finalisation sections are left to run. This is very late in the piece - too late to do any effective work.Hayse
H
9

The best way to do this (and I have spent a long time looking at this over the years) is to hook up a procedure via the AddTerminateProc() routine in SysUtils.pas. This works every time and I have been using it for years (unless you kill the process via the task manager).

Hayse answered 11/10, 2011 at 3:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.