Solved

Saving data when application exits

Posted on 2001-09-14
10
238 Views
Last Modified: 2010-04-06
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
0
Comment
Question by:filipecorreia
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
10 Comments
 
LVL 27

Accepted Solution

by:
kretzschmar earned 45 total points
ID: 6483918
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
 
LVL 1

Author Comment

by:filipecorreia
ID: 6484623
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
 
LVL 27

Expert Comment

by:kretzschmar
ID: 6484650
not the OnClose-event,
the OnCloseQuery-Event is meant

;-)
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 2

Expert Comment

by:lucika
ID: 6484892
If windows terminates the app when windows is shutdown app there comes the message WM_QUERYENDSESSION
0
 
LVL 6

Expert Comment

by:edey
ID: 6485012
But even in the case of a windows shutdown the OnCloseQuery event should still be fired - Kretzschmar is correct.

GL
Mike
0
 
LVL 1

Expert Comment

by:malsoft
ID: 6485912
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
 
LVL 3

Expert Comment

by:rondi
ID: 6486732
I'd put my code in the OnDestroy event handler.
It's sure to get called there, whatever the reason for
termination.

rondi
0
 
LVL 1

Author Comment

by:filipecorreia
ID: 6490613
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
 
LVL 1

Expert Comment

by:DelRunner
ID: 6494844
I suggest to use the Close, rather than Terminate or HALT - as these are not polite ways to end your application !
0
 
LVL 1

Author Comment

by:filipecorreia
ID: 6499209
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

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…

729 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question