-2

How I can save the content of Listbox to file When the computer shutting down or sleeping, or restarting ??? I use Delphi XE7 ,

I do save the file , and I have no problem with it !

but I want to save the file when computer shutting down .

update my code and Problem:

my problem which is , when my project run in the background the both events OnClose & OnDestroy dose not work!

If the project work normally "not in the background", the both event's is work fine!

I figure my problem , which is my project working in background process , i add this lines to do this Application.MainFormOnTaskbar := False; Application.ShowMainForm := False; If i make my project to run in back ground process the events onClose and onDestroy is definitely not work,

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
var
str :TStringList;
i : integer;
begin
  str := TStringList.Create;
  for i := 0 to ListBox1.Count-1 do
    str.Add(ListBox1.Items.Strings[i]);

  try
    str.SaveToFile('D:\test1.txt', TEncoding.UTF8);
  finally
    str.Free;
  end;
end;

procedure TForm1.FormDestroy(Sender: TObject);
var
str :TStringList;
i : integer;
begin
  str := TStringList.Create;
  for i := 0 to ListBox1.Count-1 do
    str.Add(ListBox1.Items.Strings[i]);

  try
    str.SaveToFile('D:\test15.txt', TEncoding.UTF8);
  finally
    str.Free;
  end;
end;
J...
  • 30,968
  • 6
  • 66
  • 143
Adam
  • 78
  • 2
  • 11
  • 2
    Related: http://stackoverflow.com/questions/25536216 – Kromster Mar 24 '16 at 09:28
  • When the system shuts down, your app will be closed and you can save the file then. Likewise for restarting. I presume that you want to save the file when the app closes, rather than just when the system goes down. Sleep is different. You'll need to detect that another way. – David Heffernan Mar 24 '16 at 09:40
  • Mr David , I try to save file , On destroy event, and On close event ! and it's didn't work ! – Adam Mar 24 '16 at 09:58
  • @Adam What did you try? Show us your code - we can't see your screen. "Didn't work" is not very helpful. What happened? Did the code not compile? Did it execute? Were there errors? Exceptions? – J... Mar 24 '16 at 10:08
  • 1
    @DavidHeffernan , I figure my problem , which is my project working in background process , i add this lines to do this `Application.MainFormOnTaskbar := False;` `Application.ShowMainForm := False;` If i make my project to run in back ground process the events `onClose` and `onDestroy` is definitely not work, I update my question and put mycode. – Adam Mar 24 '16 at 10:55
  • You didn't answer my question about whether or not you want the listbox to be saved when the app closes normally. I cannot understand why you would use a listbox in a non-visual app. I have not got the energy to help you work out what your question really is. Sorry. – David Heffernan Mar 24 '16 at 11:00
  • maybe it's a mistake to use listbox , I can use TstringList to do my job, I use listbox or stringList to delete every lines I searched for it , and every computer shut down I want to save the listbox or stringlist contents to file if search process not finish ! Is it clear ?? :) – Adam Mar 24 '16 at 11:06
  • 1
    Not really. I cannot understand why you are happy to let the program close without taking action, but want to do something when the system shuts down. I don't understand why you won't read MSDN docs. And I don't want to spend more time fighting. – David Heffernan Mar 24 '16 at 11:34
  • I couldn't answer it because it was incomplete and you still didn't address all of my questions. You should be more forthcoming. – David Heffernan Mar 24 '16 at 12:59
  • How does a user close your application normally? – Sertac Akyuz Mar 25 '16 at 10:31

1 Answers1

2

Handle the WM_ENDSESSION message and save your file there.

Catch the windows message like this:

private
    procedure OnShutDown(var Msg: TMessage); message WM_ENDSESSION;

And here is your implementation

procedure TForm1.OnShutDown(var Msg: TMessage);
begin
    //Save your file here. 
end;
Charlie
  • 22,886
  • 11
  • 59
  • 90
  • 1
    Do you know what a windows message is? Do you know what an `if` statement is? Did you read the documentation for `WM_QUERYENDSESSION`? – David Heffernan Mar 24 '16 at 09:54
  • 2
    @Charlie Docs say *When an application returns TRUE for this message, it receives the WM_ENDSESSION message, regardless of how the other applications respond to the WM_QUERYENDSESSION message. Each application should return TRUE or FALSE immediately upon receiving this message, and defer any cleanup operations until it receives the WM_ENDSESSION message.* In other words, don't save the file in the handler for `WM_QUERYENDSESSION`. This is much more complicated than your answer implies. – David Heffernan Mar 24 '16 at 09:55