Can't close my browser window and subprocess stays alive

Post Reply
sebfischer83
Posts: 10
Joined: Mon Nov 30, 2020 6:37 am

Can't close my browser window and subprocess stays alive

Post by sebfischer83 »

Hi,

I had different CEF windows in my application.
At one point when a form gets closed I want to close that specific CEF instance.
I looked at the documentation but it's different in different examples.

I had this procedures:

Code: Select all

procedure TForm_ChromeRepdoc.Chromium1Close(Sender: TObject; const browser: ICefBrowser; var aAction : TCefCloseBrowserAction);
begin
  if (browser <> nil) and
     (ChromiumWindow1.ChromiumBrowser.BrowserId = browser.Identifier) and
     (ChromiumWindow1 <> nil) then
    begin
      PostMessage(Handle, CEFBROWSER_DESTROY, 0, 0);
      aAction := cbaDelay;
    end;
end;

procedure TForm_ChromeRepdoc.Chromium1BeforeClose(Sender: TObject; const browser: ICefBrowser);
begin
  // The main browser is being destroyed
  if (ChromiumWindow1.ChromiumBrowser.BrowserId = 0) then
    begin
      FCanClose := True;
      PostMessage(Handle, WM_CLOSE, 0, 0);
    end;
end;

procedure TForm_ChromeRepdoc.FormCloseQuery(Sender: TObject;
  var CanClose: Boolean);
begin
  try
      if BestellId <> TGUID.Empty then
      begin
        try
        TThread.Queue(nil,
        procedure
        begin
          DatensatzVeraendertBestellung();
        end);

        except on E: Exception do
        end;
      end;
  finally
      BestellId := TGuid.Empty;
  end;
  CanClose := FCanClose;

  if not(FClosing) then
    begin
      FClosing := True;
      Visible  := False;

      ChromiumWindow1.ChromiumBrowser.CloseAllBrowsers();
    end;
end;
But Chromium1Close gets called but Chromium1BeforeClose never.
User avatar
salvadordf
Posts: 3067
Joined: Thu Feb 02, 2017 12:24 pm
Location: Spain
Contact:

Re: Can't close my browser window and subprocess stays alive

Post by salvadordf »

Hi,

TChromiumWindow is only intended for extremely simple browsers.

If your application needs to show several child forms with a browser on each of them I would recommend that you use the code in the ToolBoxBrowser demo.

ToolBoxBrowser has some extra code that closes the browser if the user presses the ESC key and the form borders are fixed but you can change all that easily.

Read the code comments in uMainForm.pas and uChildForm.pas

Each child form follows the usual destruction steps from the simpleBrowser2 demo and they also send a CEFBROWSER_CHILDDESTROYED message to the main form to check if the main form should also be closed in case there are no other child form alive.
sebfischer83
Posts: 10
Joined: Mon Nov 30, 2020 6:37 am

Re: Can't close my browser window and subprocess stays alive

Post by sebfischer83 »

But can I use ChromiumWindow at the same time on different parts (this part don't get closed the whole time) and make the other thing in this window?
User avatar
salvadordf
Posts: 3067
Joined: Thu Feb 02, 2017 12:24 pm
Location: Spain
Contact:

Re: Can't close my browser window and subprocess stays alive

Post by salvadordf »

The previous code is using a TChromiumWindow component with the destruction sequence of the MiniBrowser demo, which uses TChromium and TCEFWindowParent and it has TChromium.MultiBrowserMode set to True.

That destruction sequence can't be used with the original TChromiumWindow component and this is the reason the browser is not fully destroyed, causing the shutdown problems.

I haven't tried to enable MultiBrowserMode in a TChromiumWindow browser but I guess it would reqiere many code changes to that component.

If you use MultiBrowserMode then consider copying the code from the MiniBrowser demo and replacing TChromiumWindow with a TChromium and a TCEFWindowParent component.

If you just need to open child forms with independent browsers then use the ToolBoxBrowser demo I mentioned.
Post Reply