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.

Question: Initializing CEF on demand

Post Reply
TobiasM
Posts: 4
Joined: Mon Dec 14, 2020 9:09 am

Question: Initializing CEF on demand

Post by TobiasM »

hi all,
I have questions regarding the initialization of the GlobalCEFApp.

What I try to achieve is to only start the additional processes of CEF when I really need them. Furthermore I need to run the Forms with the Browsers on it as MDI-childs.
I tryed to use the DLLBrowser-demo as a reference but is it even possible to use a DLL to initialize CEF and create the Forms as MDI-childs?
Is initializing CEF in a dpr the only way?

Best regards
TobiasM
User avatar
salvadordf
Posts: 4047
Joined: Thu Feb 02, 2017 12:24 pm
Location: Spain
Contact:

Re: Question: Initializing CEF on demand

Post by salvadordf »

Hi,

You can move the CEF initialization out of the DPR file but then you would need to use a different EXE for the CEF subprocesses. You have an example in the SubProcess demo.

However, CEF can only be initialized once per process. It's not possible to load-unload CEF several times. We need to initialize CEF once and then create and destroy as many browsers as the application needs.
TobiasM
Posts: 4
Joined: Mon Dec 14, 2020 9:09 am

Re: Question: Initializing CEF on demand

Post by TobiasM »

Hi and thanks for the answer!
you would need to use a different EXE for the CEF subprocesses
yes as I wrote I want to have my forms as MDI-childs of my main program so this is not a possibility right?
CEF can only be initialized once per process. It's not possible to load-unload CEF several times
I know and I dont try to do this.

I will repeat myself but: I want to start the CEF processes on demand. Not in the dpr of my main program. Its fine to start it in a dll and finizile it when the program is closed. And I need to have the forms with chromium components on as MDI childs of my main program. Is that possible to achieve?
User avatar
salvadordf
Posts: 4047
Joined: Thu Feb 02, 2017 12:24 pm
Location: Spain
Contact:

Re: Question: Initializing CEF on demand

Post by salvadordf »

TobiasM wrote: Wed Dec 16, 2020 7:09 am Hi and thanks for the answer!
you would need to use a different EXE for the CEF subprocesses
yes as I wrote I want to have my forms as MDI-childs of my main program so this is not a possibility right?
Yes, it's possible to embed browsers in MDI forms. Take a look at the MDIBrowser demo.
TobiasM wrote: Wed Dec 16, 2020 7:09 am I will repeat myself but: I want to start the CEF processes on demand. Not in the dpr of my main program. Its fine to start it in a dll and finizile it when the program is closed. And I need to have the forms with chromium components on as MDI childs of my main program. Is that possible to achieve?
Chromium handles the number of processes internally. We can only create and destroy browsers. Navigating with those browsers will create new processes as needed. In fact, CEF will create 2 extra processes right after initialization.

When the user closes the browser then Chromium will decide what to do with the render processes used by that browser. It will close them eventually or maybe reuse them...I'm not sure.

You can delay the CEF initialization and create MDI forms with browsers but using a DLL for that can be problematic because GlobalCEFApp and the browsers would be isolated from each other. I would suggest that you use a different approach.

You can wait until the main form is shown to initialize CEF in a secondary PAS unit and use the GlobalCEFApp.OnContextInitialized event to know when you can create new MDI forms with browsers. The MDIBrowser and the SubProcess demos show how to do that.
TobiasM
Posts: 4
Joined: Mon Dec 14, 2020 9:09 am

Re: Question: Initializing CEF on demand

Post by TobiasM »

You can wait until the main form is shown to initialize CEF in a secondary PAS unit and use the GlobalCEFApp
But how when I initialize it in the initialization section like its done in the SubProcess-Demo?
Also, even that would be too early. I have to initialize CEF in the moment I need it. As I hit the button "ShowBrowser" and not when the main form is shown.
I would prefer to not use a dll too. I dont care if I use a blank exe for the subprocesses or the main exe.
All I want is to start my program as it always did without additional processes and when I need them (hit "ShowBrowser") it is totally fine to have them around until I end my program. I just cant get that to work.
User avatar
salvadordf
Posts: 4047
Joined: Thu Feb 02, 2017 12:24 pm
Location: Spain
Contact:

Re: Question: Initializing CEF on demand

Post by salvadordf »

You can create GlobalCEFApp, set its properties and call GlobalCEFApp.StartMainProcess inside the "OnClick" event of the "ShowBrowser" button.
Use the GlobalCEFApp.OnContextInitialized event to send a Windows message to the main form to create the MDI form in the main application thread.

If the user clicks the "ShowBrowser" button a second time then the OnClick event should create the MDI form directly.
TobiasM
Posts: 4
Joined: Mon Dec 14, 2020 9:09 am

Re: Question: Initializing CEF on demand

Post by TobiasM »

Good morning,

first and foremost thanks for the answer, it was really that easy and suited my needs!
But I was confused. Why do you initialize CEF the way you do in the dpr's in all of your demos? It implicates that it has to be this way. For me at least. Is there a benefit to do it like that or a problem when doing it somewhere else?
I cant really see why you want to tamper with your dpr when you can just have it super simple in e.g. an OnClick event?
User avatar
salvadordf
Posts: 4047
Joined: Thu Feb 02, 2017 12:24 pm
Location: Spain
Contact:

Re: Question: Initializing CEF on demand

Post by salvadordf »

Many developers prefer to use only one EXE in their applications and using the DPR is the only way to achieve that.

Chromium runs the same EXE for the subprocesses and it's necessary to intercept those executions before the application is initialized.
Post Reply