Page 1 of 1

Destroying CEFWindowParent1 in the main thread cannot trigger TChromium.onClose

Posted: Thu Aug 15, 2024 2:34 am
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;



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

Posted: Fri Aug 16, 2024 5:50 am
by dilfich
Check out the DEMO, for example MDIBrowser.

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

Posted: Fri Aug 16, 2024 7:01 am
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.