Solved

Saving data when application exits

Posted on 2001-09-14
10
234 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
10 Comments
 
LVL 27

Accepted Solution

by:
kretzschmar earned 45 total points
Comment Utility
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
Comment Utility
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
Comment Utility
not the OnClose-event,
the OnCloseQuery-Event is meant

;-)
0
 
LVL 2

Expert Comment

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

Expert Comment

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

GL
Mike
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 1

Expert Comment

by:malsoft
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
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 video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

772 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now