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.
If you find these projects useful please consider becoming a sponsor with Patreon, GitHub or Liberapay.

Destroying CEFWindowParent1 in the main thread cannot trigger TChromium.onClose

Post Reply
dreamnyj
Posts: 2
Joined: Sat Sep 10, 2022 9:00 am

Destroying CEFWindowParent1 in the main thread cannot trigger TChromium.onClose

Post by dreamnyj »

I want to safely close the browser process
My program has opened a total of 5 browser processes.
I will use the following standard method to close the browser process.
The other four are working normally, but one is executing CEFWindowParent1.Free;
Afterwards, Chromium1BeforeClose() will not be triggered and the reason cannot be found. I hope to receive some guidance. Thank you.


// Destruction steps
// =================
// 1. FormCloseQuery sets CanClose to FALSE calls TChromium.CloseBrowser which triggers the TChromium.OnClose event.
// 2. TChromium.OnClose sends a CEF_DESTROY message to destroy CEFWindowParent1 in the main thread, which triggers the TChromium.OnBeforeClose event.
// 3. TChromium.OnBeforeClose sets FCanClose := True and sends WM_CLOSE to the form.

Code: Select all

1、FormCloseQuery sets CanClose to FALSE calls TChromium.CloseBrowser

Chromium1. CloseBrowser (True) ;

2、trigger

procedure TForm1.Chromium1Close(Sender: TObject;
const browser: ICefBrowser; var aAction : TCefCloseBrowserAction);
begin
PostMessage(Handle, CEF_DESTROY, 0, 0);
aAction := cbaDelay;
end;


3、Message triggered
procedure TForm1.BrowserDestroyMsg(var aMessage: TMessage);
begin
    CEFWindowParent1.Free;
end;


4、Final trigger:

procedure TForm1.Chromium1BeforeClose(Sender: TObject;
const browser: ICefBrowser);
begin
FCanClose := True;
PostMessage(Handle, WM_CLOSE, 0, 0);
end;


dilfich
Posts: 368
Joined: Thu Nov 30, 2017 1:17 am

Re: Destroying CEFWindowParent1 in the main thread cannot trigger TChromium.onClose

Post by dilfich »

Check out the DEMO, for example MDIBrowser.
dreamnyj
Posts: 2
Joined: Sat Sep 10, 2022 9:00 am

Re: Destroying CEFWindowParent1 in the main thread cannot trigger TChromium.onClose

Post by dreamnyj »

dilfich wrote: Fri Aug 16, 2024 5:50 am Check out the DEMO, for example MDIBrowser.
I carefully reviewed the code of MIDI Browser and it is the same as my handling of events.
I'm also puzzled why I opened 6 browser processes, but everyone else can call this onBeforeClose event
My current approach is to skip the onBeforeClose event and go directly to
In the BrowserDestroy event, set FCanLoss to True;
PostMessage(Handle, WM_CLOSE, 0, 0);
At present, it seems to be working normally. It can be considered as a detour solution.
Thank you for your guidance.
Post Reply