D
D
DeeUs2021-05-30 06:06:08
Delphi
DeeUs, 2021-05-30 06:06:08

How to close the program correctly?

Hello!
How to close the program correctly?
Now it works for me in such a way that when I click the cross on the form, it collapses to tray, and the close button itself is implemented in the PopupMenu:

procedure TForm1.FormCloseQuery(Sender: TObject; var CanClose: Boolean);  
begin
//не закрываем а сворачиваем в трей
  CanClose := false;
  TrayIcon1.Visible := true;
  Form1.Visible := false;
  ShowWindow(Application.Handle, SW_HIDE);
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
  UnRegisterHotKey (Form1.Handle, 0);
  TrayIcon1.Free;
  Image1.Free;
  Self.Free;
end;
procedure TForm1.N4Click(Sender: TObject);   //кнопка закрыть 
begin
  //halt;
  Application.Terminate;
end;
procedure TForm1.ApplicationEvents1Minimize(Sender: TObject);       //сворачивание окна
begin
  Form1.Visible := false; // Делаем форму невидимой
  TrayIcon1.Visible := True; // Делаем иконку с трее видимой
end;

Everything seems to be working, but when the program is closed in windows 10, the sound signal "Critical error" is played, which in turn suggests: "I did something wrong ...".
Please explain how to do it right and where is my mistake (if any).

PS:
The main form contains the following components: ApplicationEvents, PopupMenu, TrayIcon, ImageList, ColorDialog, SavePictureDialog, OpenPictureDialog, IdHTTP, IdAntiFreeze, IdSSLIOHandlerSocketOpenSSL + all sorts of panel buttons, etc.
There are 5 forms in the program itself: main, settings (IdHTTP, IdSSLIOHandlerSocketOpenSS), authorization form, richedit editor form and additional form a la modal window.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
H
Hemul GM, 2021-05-30
@DeeUs

1. You don't need to destroy/release components that are created through the designer or with an Owner. Such components will free themselves.
2. FormDestroy - called when the form is destroyed, and you are again trying to cause the form to be destroyed. It can even be logically understood.
3. Application Terminate; - normal closing of the program. This method sets a flag to terminate the program. And the program will close as soon as the message queue ends.

O
OCTAGRAM, 2021-05-30
@OCTAGRAM

First, it's safer and more portable to do FreeAndNil instead of calling Free directly. Second, clean up immediately
Self.Free;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question