Saving data when application exits

the aim is to save (to file) some data when the application exits.
 I want this to happen even if windows terminates the app when
 windows is shutdown. Perhaps I could catch the quit message sent
 to the app but I don't have any idea on how to do that.



   Thanks in advance

         Filipe Correia
LVL 1
filipecorreiaAsked:
Who is Participating?
 
kretzschmarCommented:
just assign a procedure to the onclosequery-event of your (main)-form,
where you can check, save or abort like

procedure TForm1.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
var mresult : Integer;
begin
  if memo1.Modified then
  begin
    mResult := MessageBox(self.Handle,'Save before Quit?','Confirm',MB_YESNOCANCEL or MB_ICONQUESTION);
    case mResult of
      mrYes : If savedialog1.execute then
                memo1.lines.savetofile(savedialog1.filename);
      mrCancel : CanClose := False;
    end;
  end;
end;

meikl ;-)
0
 
filipecorreiaAuthor Commented:
but... is the form's OnClose event trigered in case the main form is always hiden (by doing Application.ShowMainForm:=false) ? I think it isn't because when I put ShowMessage("hey"); in the OnClose event no message is shown when the app exits.


  Filipe Correia
0
 
kretzschmarCommented:
not the OnClose-event,
the OnCloseQuery-Event is meant

;-)
0
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

 
lucikaCommented:
If windows terminates the app when windows is shutdown app there comes the message WM_QUERYENDSESSION
0
 
edeyCommented:
But even in the case of a windows shutdown the OnCloseQuery event should still be fired - Kretzschmar is correct.

GL
Mike
0
 
malsoftCommented:
Also, I was reading a few of the TI's on Borland's website the other day and they stated that if you do have a handler for the OnCloseQuery and set the value of CanClose to False, Windows shutdown can be halted.
0
 
rondiCommented:
I'd put my code in the OnDestroy event handler.
It's sure to get called there, whatever the reason for
termination.

rondi
0
 
filipecorreiaAuthor Commented:
I have an option in my app that terminates the app by doing:

      Application.Terminate()

When I do that the OnCloseQuery isn't trigered.
To solve this I tried subtitute this code by:

        Form1->Close();

and now everything seems to be working fine(I'll test it some more times).

Do you predict any sort of problem that may hapen by doing this?



rondi: I will try your suggestion but I haven't tried it yet.


    Filipe Correia
0
 
DelRunnerCommented:
I suggest to use the Close, rather than Terminate or HALT - as these are not polite ways to end your application !
0
 
filipecorreiaAuthor Commented:
Hi

rondi: An EAccessViolation happens if I use the OnDestroy event. I don't know why!

I'll use the onCloseQuery event. My app seems to be working ok until now.


  Thanks to you all

        Filipe Correia
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.