In Delphi 7, I have a TMainForm.FormClose procedure that is intended to write out some status whenever the program exits. This works fine when manually closing the program. However, I discovered that if the program is "forced" to exit by Windows (for example after Windows Update that requires a reboot), the FormClose procedure is not called.
Edit - I'm new here, and it looks like I can't delete my own post. After some more searching, I found a solution.
This one's real difficult. If you kill a process externally, that's instant termination. No chance to clean up, period. Windows Update's reboots often close a program "right" by sending it a WM_QUERYENDSESSION message, but it doesn't always work for whatever reason. Particularly, if your program delays for very long, (asking the user if he wants to save before quitting, for example,) the shutdown code will kill the process.
So if you want to guarantee that it'll always call that event handler, you'll have to guarantee that you never use a modal dialog box or anything else that would block the program from immediately bailing out when it receives WM_QUERYENDSESSION. This is probably more trouble than it's worth.
One alternative would be to do something similar to what Firefox does: Write the status data out to a temporary file while it's still running, and then when it restarts, check to see if that file's there and if the data indicates it's still in an "open" state. If so, your program can know that its last incarnation got killed somehow and take whatever action is appropriate, such as updating the status log (or whatever you're using) with the last available data.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With