title: When computer take order to shutdown , I need to save file
body: <p>How I can save the content of Listbox to file When the computer <strong>shutting down or sleeping, or restarting</strong> ???
  I use Delphi XE7 ,</p>
  
  <p>I do save the file , and I have no problem with it !</p>
  
  <p>but I want to save the file when computer shutting down .</p>
  
  <p><strong>update my code and Problem:</strong></p>
  
  <p>my problem which is , when my project run in the background the both events OnClose &amp; OnDestroy dose not work!</p>
  
  <p>If the project work normally "not in the background", the both event's is work fine!</p>
  
  <p>I figure my problem , which is my project working in background process , i add this lines to do this <code>Application.MainFormOnTaskbar := False;</code> <code>Application.ShowMainForm := False;</code> If i make my project to run in back ground process the events <code>onClose</code> and <code>onDestroy</code> is definitely not work,</p>
  
  <pre><code>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;
  </code></pre>