Cef initialize Threading Callback

Having problems with building or using CEF's C/C++ APIs? This forum is here to help. Please do not post bug reports or feature requests here.

Cef initialize Threading Callback

Postby IvanPong1 » Mon Jun 21, 2021 6:28 pm

Hi, I'm writing a CEF C++ app with
Code: Select all
   CefSettings settings = CefSettings();
   settings.size = sizeof(CefSettings);
   settings.log_severity = LOGSEVERITY_DEBUG;
   settings.multi_threaded_message_loop = true;
   //we need to get the filepath.
   LPWSTR wPath = new WCHAR[MAX_PATH + 1];
   GetModuleFileName(GetModuleHandle(NULL), wPath, MAX_PATH + 1);
   //currently this looks like the full path of this exe, we need to change it to the other one.
   std::wstring wP(wPath);
   auto point = wP.find_last_of(L"\\");
   std::wstring realPath = wP.substr(0, point + 1) + L"VPHTMLManagerSubProcess.exe";
   cef_string_set(realPath.c_str(), std::wcslen(realPath.c_str()), &(settings.browser_subprocess_path), true);
   delete[] wPath;
   settings.windowless_rendering_enabled = true;
   settings.no_sandbox = true;

   // Initialize CEF - other modules will conflict
   CefInitialize(main_args, settings, NULL, NULL);


and initializing a browser with:
Code: Select all
   CefWindowInfo window_info;
   window_info.SetAsWindowless(nullptr);
   CefBrowserSettings browser_settings = CefBrowserSettings();
   browser_settings.size = sizeof(CefBrowserSettings);
   browser_settings.webgl = STATE_DISABLED;
   browser_settings.windowless_frame_rate = 24;
   browser_settings.background_color = 0x50505050;

   CefBrowserHost::CreateBrowser(window_info, this, CefString("https://www.bing.com/"), browser_settings, NULL, NULL);



I'm using the multi_threaded_thread_loop on windows. For a while I couldn't figure out why the first couple browsers I created wouldn't render anything. Eventually I figured out that there's no queue that Browser creation is put on after CefInitialize is called, so browsers constructed too soon won't render. Currently, I have this fixed by having my main thread sleep for a bit, before constructing a browser. Is there a better way of doing this, like a callback I can subscribe to which would notify me when it's safe to create a browser?

Also, currently, after a browser has had OnAfterCreated called, I allow it to receive LoadURL requests. Is this the only requirement to start sending LoadURL requests, or are there other callbacks I should listen to? Also, after a LoadURL request is sent, is it safe to send LoadURL requests immediately after that, or should I wait for the Loading State to change. Finally, when the dimensions of the browser are changed, are there any requirements I should be aware of, other than OnAfterCreate being called for making an WasResized call into the browser?

That's a lot of questions, but making sure that everything is properly sequenced when switching from the main threaded approach to this multi threaded approach has been kind of frustrating due to the number of mistakes I've made. Also, in the past I've had rare errors that I could never reproduce due to race conditions with the browser, and this time around I want to avoid that type of issue.

Thank you.
IvanPong1
Techie
 
Posts: 11
Joined: Thu Aug 20, 2020 10:55 pm

Re: Cef initialize Threading Callback

Postby amaitland » Mon Jun 21, 2021 6:40 pm

Maintainer of the CefSharp project.
amaitland
Master
 
Posts: 965
Joined: Wed Jan 14, 2015 2:35 am

Re: Cef initialize Threading Callback

Postby IvanPong1 » Tue Jun 22, 2021 12:29 pm

Hi Amailand,

Thanks for the response. I implemented OnContextInitialized, but, as before, I have to insert a sleep command into it before calling CreateBrowser. Otherwise, the first browser I create is apparently too soon, and won't load anything.

Thanks
IvanPong1
Techie
 
Posts: 11
Joined: Thu Aug 20, 2020 10:55 pm

Re: Cef initialize Threading Callback

Postby magreenblatt » Tue Jun 22, 2021 2:55 pm

What OS and CEF version? Does the problem reproduce with the CEF sample apps?

With current CEF versions the CreateBrowser execution should be delayed until after the context has initialized. You appear to be using off-screen rendering, so make sure you’re meeting the requirements related to that.
magreenblatt
Site Admin
 
Posts: 10930
Joined: Fri May 29, 2009 6:57 pm

Re: Cef initialize Threading Callback

Postby IvanPong1 » Tue Jun 22, 2021 4:43 pm

I'm currently on Windows 10. The libcef.dll file shows a version of 87.1.12+g03f9336+chromium....
I used to work with CefSharp, and from trying those programs, I can fairly safely say I was not able to reproduce in those cases.
I'm less familiar with the Cef example projects. Most of the ones I saw don't use the multi_threaded_message_loop with offscreen rendering.

Is there one that does? If so, I'd be happy to modify it to try to replicate my problem. I spent most of my time referring to this:
https://github.com/daktronics/cef-mixer
Though, I don't use accelerated rendering, and have all GPU stuff turned off.



Currently, I can verify that I am still using the options I used in my first post, with the difference now that I make a CefRefPtr to a CefApp instance
and feed it in with a callback to OnContextInitilized. On startup, I get a single breakpoint hit in OnContextInitialized and immediately create a browser with the parameters I've listed.

A single call to onPaint gets made rendering a grey screen, grey being the background color I chose. I enabled debug level logging, but what to look for is opaque to me.


Thanks for the help!
IvanPong1
Techie
 
Posts: 11
Joined: Thu Aug 20, 2020 10:55 pm

Re: Cef initialize Threading Callback

Postby magreenblatt » Tue Jun 22, 2021 4:52 pm

You can run cefclient with `--multi-threaded-message-loop --off-screen-rendering-enabled`. I suggest testing with currently supported versions.
magreenblatt
Site Admin
 
Posts: 10930
Joined: Fri May 29, 2009 6:57 pm

Re: Cef initialize Threading Callback

Postby IvanPong1 » Mon Jun 28, 2021 7:32 pm

Thanks. I took a look at the example, and the way it initializes is fundamentally different. I don't have to wait for window creation, so I call CreateBrowser directly in the OnCefInitialized callback. While the cefclient program waits for the window initialization after CEF has been initialized and then dispatches a task to create the browser.

I can put together a minimal example project on git by the 30th, if you'd be willing to take a look. The cefclient application is too different from what I do for me to modify it directly, but I'm able to reproduce my issue consistently. I'll aim to make it self-packaged as well. Though first I'd like to here back on if anything is fundamentally wrong with me calling CreateBrowser in OnCefInitialized, since no one else seems to be doing it.



Thanks
IvanPong1
Techie
 
Posts: 11
Joined: Thu Aug 20, 2020 10:55 pm


Return to Support Forum

Who is online

Users browsing this forum: Google [Bot] and 13 guests