Board index » delphi » Windows exit terminates delphi app rudely

Windows exit terminates delphi app rudely

If a user is running my delphi APP and exits windows without first
exiting the Delphi app, the app just gets vaporized. Its an abnormal exit,
 files are left in funny states etc. I would like it to behave as if the
user double clicked the upper left hyphen bar in the APP's window.

Does anyone know how to:
 1. make my app realize windows wants to shut down so it can self
destruct gracefully?

2. or barring that, set a flag that prevents windows from exiting if my
APP is still runnning.

Thanks

 

Re:Windows exit terminates delphi app rudely


Quote
Samuel Washburn (GGHG...@prodigy.com) wrote:
> Does anyone know how to:
>  1. make my app realize windows wants to shut down so it can self
> destruct gracefully?

Two form events fire when the application is about to be shut down:

OnClose
OnCloseQuery

Check the online help.  You can cancel the action if you need to save
files or whatever, or you can just clean up everything and then gracefully
exit.

Quote
> 2. or barring that, set a flag that prevents windows from exiting if my
> APP is still runnning.

In the OnCloseQuery event handler, you simply set CanClose to False.

Ken Bolingbroke
hbdef...@huey.csun.edu
http://www.csun.edu/~hbdef045
\,,/_

Re:Windows exit terminates delphi app rudely


Thanks. Your suggestions work but there are some subtleties here that are
worth knowing about:

Onclosequery and Onclose events fire in that order if you do any of the
following:
  issue a pascal "CLose" statement;
  Close the app via the task manager End task;
  Chose "Close" from the applications System Menu (Hyphen menu).

However if you just exit from windows with out doing any of the above,
only the OnCloseQuery event fires! For some reason Onclose does NOT fire!
This seems like a bug. I worked around it by calling the FormClose
procedure (the OnClose Event handler) from the OnCloseQuery eventhandler.
 This is a little hokey because this way the FormClose procedure executes
twice if you exit in any of the three ways listed above!. It executes
just once if you exit by closing down windows itself!

Re:Windows exit terminates delphi app rudely


In <43kvg4$...@dewey.csun.edu> hbdef...@huey.csun.edu (shauna bolingbroke) writes:

Quote
>Samuel Washburn (GGHG...@prodigy.com) wrote:
>> Does anyone know how to:
>>  1. make my app realize windows wants to shut down so it can self
>> destruct gracefully?
>Two form events fire when the application is about to be shut down:
>OnClose
>OnCloseQuery
>Check the online help.  You can cancel the action if you need to save
>files or whatever, or you can just clean up everything and then gracefully
>exit.
>> 2. or barring that, set a flag that prevents windows from exiting if my
>> APP is still runnning.
>In the OnCloseQuery event handler, you simply set CanClose to False.

Yes. But if Windows itself is shutting down, OnClose will _not_ be called.
A solution to this problem is to handle the WM_EndSession message from
Windows by calling Halt. This will call all exitprocs, cleaning things up
after BDE etc. Use Application.HookMainWindow to handle the WM_EndSession
message. An example of how to do this will (probably) surface in the next
issue of The Delphi Magazine (contact the editor Chris Frizielle at
70630....@compuserve.com).

--
 Hallvard Vassbotn  | Falcon AS (a Reuters company) | Without programmers,
 hallv...@falcon.no | Stranden 1, 0250 OSLO, Norway | the world would stop!

Re:Windows exit terminates delphi app rudely


In <43mjia$...@usenetp1.news.prodigy.com> GGHG...@prodigy.com (Samuel Washburn) writes:

Quote
>Thanks. Your suggestions work but there are some subtleties here that are
>worth knowing about:
>Onclosequery and Onclose events fire in that order if you do any of the
>following:
>  issue a pascal "CLose" statement;
>  Close the app via the task manager End task;
>  Chose "Close" from the applications System Menu (Hyphen menu).
>However if you just exit from windows with out doing any of the above,
>only the OnCloseQuery event fires! For some reason Onclose does NOT fire!
>This seems like a bug. I worked around it by calling the FormClose
>procedure (the OnClose Event handler) from the OnCloseQuery eventhandler.
> This is a little hokey because this way the FormClose procedure executes
>twice if you exit in any of the three ways listed above!. It executes
>just once if you exit by closing down windows itself!

Not a very nice solution. If another program decides that it will not
allow Windows to close down, your program will close down when it should
not.

As said before, you can solve this bug by doing this:

procedure TForm1.FormCreate(Sender: TObject);
begin
  Application.HookMainWindow(HookProc);
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
  Application.UnHookMainWindow(HookProc);
end;

function TForm1.HookProc(var Message: TMessage): boolean;
begin
  Result := false;
  if (Message.Msg = WM_EndSession) and
     WordBool(Message.wParam)      then
    Halt;
end;

When Windows is actualy closing down, it sends a WM_EndSession message.
VCL does not handle this message. Here we add a hook to handle it
ourselves.

The cleanest solution would have been to call Application.Terminate
or MainForm.Close. Unfortunately this will not always work because these
are message based and Windows doesn't guarantee that any messages will
be processed after WM_EndSession has been sent.

So my solution is to simply call Halt. This might seem drastic, but it
actually calls all ExitProcs, that again call all destructors for all
forms closing them down properly, calling a ExitProc that close down
the BDE properly and so on.

This solution will _not_ call your OnClose handler, but it _will_ call your
OnDestroy handler. Put all your essential clean-up code there and you should
be ok.

--
 Hallvard Vassbotn  | Falcon AS (a Reuters company) | Without programmers,
 hallv...@falcon.no | Stranden 1, 0250 OSLO, Norway | the world would stop!

Other Threads