Disclosure Statement: This site contains affiliate links, which means that I may receive a commission if you make a purchase using these links. As an eBay Partner, I earn from qualifying purchases.

Closing application correctly

andreykrasnodar
Posts: 112
Joined: Wed Jul 01, 2020 10:22 am

Re: Closing application correctly

Post by andreykrasnodar »

It does not occur eny errors, but does not close application correctly. The demo.exe is still in memory after closing application.
User avatar
salvadordf
Posts: 4052
Joined: Thu Feb 02, 2017 12:24 pm
Location: Spain
Contact:

Re: Closing application correctly

Post by salvadordf »

Here's the fixed demo. All the files have changes.

The OSR browsers need to have the GlobalCEFApp.WindowlessRenderingEnabled property set to TRUE in the DPR file.

I also changed a few things in the form and the pas unit.

The code comments in the demos still make a reference to the old way of closing the browsers. Instead of setting the "Result" parameter to false in the TChromium.OnClose events you have to set "aAction" to "cbaClose" because these are OSR browsers. Sorry about that.

Copy the missing code from the SimpleOSRBrowser demo in order to show the browser contents.
You do not have the required permissions to view the files attached to this post.
andreykrasnodar
Posts: 112
Joined: Wed Jul 01, 2020 10:22 am

Re: Closing application correctly

Post by andreykrasnodar »

salvadordf wrote: Sun Aug 30, 2020 10:13 am Here's the fixed demo. All the files have changes.

The OSR browsers need to have the GlobalCEFApp.WindowlessRenderingEnabled property set to TRUE in the DPR file.

I also changed a few things in the form and the pas unit.

The code comments in the demos still make a reference to the old way of closing the browsers. Instead of setting the "Result" parameter to false in the TChromium.OnClose events you have to set "aAction" to "cbaClose" because these are OSR browsers. Sorry about that.

Copy the missing code from the SimpleOSRBrowser demo in order to show the browser contents.
Tnanks. And now I have placed TTimer component on the form and added theese strings:

Code: Select all

procedure TForm1.Timer3Timer(Sender: TObject);
var
  i:integer;
  MouseCoord:TCefMouseEvent;
begin
  timer3.Enabled:=false;
  Chromium1.LoadURL('https://yahoo.com');
  for I := 0 to 999 do
  begin
    MouseCoord.x:=random(200);
    MouseCoord.y:=random(200);
    Chromium1.SendMouseMoveEvent(@MouseCoord,false);
    sleep(500);
    application.ProcessMessages;
  end;
end;
After pressing X button (close) the demo.exe is still in memory
Student
Posts: 72
Joined: Tue Aug 07, 2018 9:20 am

Re: Closing application correctly

Post by Student »

Перед завершением работы программы ты должен сам позаботиться, что ничего не будет использовать уничтоженные объекты, в твоем случае в таймере цикл не знает что программа завершается и обращается к хромиуму, добавь туда переменную которая будет проверяться что послан сигнал завершения программы и нужно прервать цикл.
andreykrasnodar
Posts: 112
Joined: Wed Jul 01, 2020 10:22 am

Re: Closing application correctly

Post by andreykrasnodar »

Student wrote: Sun Aug 30, 2020 9:49 pm Перед завершением работы программы ты должен сам позаботиться, что ничего не будет использовать уничтоженные объекты, в твоем случае в таймере цикл не знает что программа завершается и обращается к хромиуму, добавь туда переменную которая будет проверяться что послан сигнал завершения программы и нужно прервать цикл.
Это довольно непросто сделать. Выше лишь пример и достаточно без новых переменных лишь указать if Form1.Visible, а в моей программе тысяча функций и каждой ставить условие... эх...
Странно, что в предыдущей версии я спокойно это делал функцией, кажется CEFDestroy + halt;
Student
Posts: 72
Joined: Tue Aug 07, 2018 9:20 am

Re: Closing application correctly

Post by Student »

А под отладкой ошибку какую выбивает? Внутри функции есть проверка, через переменную Initialized. Тебе нужно просто сделать так чтоб циклы прерывались после нажатия на закрытие.

Code: Select all

procedure TChromiumCore.SendMouseMoveEvent(const event: PCefMouseEvent; mouseLeave: Boolean);
begin
  if Initialized then
    Browser.Host.SendMouseMoveEvent(event, mouseLeave);
end;
Student
Posts: 72
Joined: Tue Aug 07, 2018 9:20 am

Re: Closing application correctly

Post by Student »

Halt это крайняя мера, она вынуждает резкое завершение текущего приложения и не гарантируется, что ресурсы будут освобождены при вызове halt.
andreykrasnodar
Posts: 112
Joined: Wed Jul 01, 2020 10:22 am

Re: Closing application correctly

Post by andreykrasnodar »

Can I make something like this?

Code: Select all

Chromium1.Closebrowser;
repeat
  application.processmesages;
until Chromium1.Initialized = false;
Chromium2.Closebrowser;
repeat
  application.processmesages;
until Chromium2.Initialized = false;
Application.terminate;
User avatar
salvadordf
Posts: 4052
Joined: Thu Feb 02, 2017 12:24 pm
Location: Spain
Contact:

Re: Closing application correctly

Post by salvadordf »

I would not recomend using "Application.ProcessMessages", "Halt" or "Application.terminate".

Chromium expects the application to behave asynchronously and to close the app following some steps.

Trying to do things any other way only increases the probabilities of having stability issues.
andreykrasnodar
Posts: 112
Joined: Wed Jul 01, 2020 10:22 am

Re: Closing application correctly

Post by andreykrasnodar »

Unfortunatelly, only this code works

Code: Select all

procedure TForm1.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
begin
  form1.Hide;
  if not(FClosing) then
  begin
      FClosing := True;
      Chromium1.CloseBrowser(True);
      Chromium2.CloseBrowser(True);
      repeat
        application.ProcessMessages;
      until FCanClose1 and FCanClose2;
  end;
  CanClose := FCanClose1 and FCanClose2;
end;
I do not know the reason, maybe postmessage() does not work as needed (maybe wrong handle?)
Post Reply