0

Scenario

Using Delphi 2009 on Windows 10 I have an app that performs shut down operations such as logging and database manipulation. Anywhere the user is able to exit the app I call Form.Close on the main form which calls a procedure to do the required operations. This works ok regardless of whether the user exits via the menu or by using the Windows task bar icon to close the window (as some of my users do).

Problem

When debugging the app in the IDE however, clicking the red square in the Delphi menu bar to stop the app running does not call Form.Close and hence doesn't call my shut down procedure.

Question

Is there a way to make the app correctly call form.close when shut down via the IDE so that I can ensure all parts get debugged?

Other info

I've looked here which describes a method using WMQueryEndSession to catch windows shut down messages but when I use that code in my app it is not called when the IDE shuts down the application, so maybe the IDE is using a different method.

Code used in my WMQueryEndSession procedure

procedure TFrmMain.WMQueryEndSession(var Msg: TWMQueryEndSession);
  {intercept the shut down message and call our proc first 
   SafeFormClose is very simple and quick so no issues. 
   Also called first in case the inherited procedure shuts us down
   before SafeFormClose has a chance to run}
begin
  SafeFormClose;    //Do our shut down and logging stuff
  Msg.Result := 1;  //Signal that it is OK for Windows to shut us down
  inherited;        //Do whatever the original handler would have done.
end;
user2834566
  • 775
  • 9
  • 22

1 Answers1

1

If you are running the program in debugger, clicking the red square (reset button) terminates the program immediately. The debugger has full control of the program, and no more code is run after you click the reset button.

This is different than for example terminating the program in the task manager (in which case Windows would send a close signal to the program, which could reject it if it doesn't want to close yet).

You should design the program so that if it stops at any point, the next time it is started it fixes any problems caused by the reset (for example fixes corrupted database files). This is a real situation that can occur when power is lost or the operating system crashes. You cannot trust that your shutdown routine can be run every time.

VLL
  • 9,634
  • 1
  • 29
  • 54
  • Ok. Now I understand. In a way it's a good thing that it shuts down immediately as it gets you out of things like infinite loops. No serious issues will occur in my app if the graceful shut down doesn't happen. The connection to the database will time out anyway, even if it is not explicitly closed and the logging isn't vital. My question was really about being able to check that code will be run, with the right values, in the event of an unusual shutdown but I guess I can find that out by running the exe instead. Accepted your answer as at least it answers the question! – user2834566 Feb 11 '22 at 14:10