Browser freezes while executing native code

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.

Browser freezes while executing native code

Postby johnh » Sun Aug 12, 2018 4:22 pm

Hi,

So I followed the guides / general usage around this.. I have a JS function, which calls a native c++ function.
The Execute method receives the call in the RENDERER process, I then post an IPC message to the BROWSER process which executes the actual C++ method and then posts another IPC back to the RENDERER process to initial the JS callback.
It all works, I've stepped through it and have verified that at each point I'm in the correct process and thread, however while the C++ function is running (it takes up to 30seconds) the browser view has frozen. I thought the whole point of the IPC
and executing the native code in the browser process was to allow V8/rendering/events etc to continue smoothly ?

Is there a trick I'm missing?
johnh
Techie
 
Posts: 44
Joined: Wed Aug 01, 2018 12:14 pm

Re: Browser freezes while executing native code

Postby HarmlessDave » Sun Aug 12, 2018 7:09 pm

What OS and what version of CEF?

In Windows, the browser process runs the main UI thread for the application so if you receive an event and want to start a long-running task you should spawn a worker thread and run it there.
HarmlessDave
Expert
 
Posts: 370
Joined: Fri Jul 11, 2014 2:02 pm

Re: Browser freezes while executing native code

Postby johnh » Mon Aug 13, 2018 2:40 am

Windows 10 x64, Latest CEF build.

I suspected as much. My understanding was this:

The PID_RENDERER is handling V8/events/rendering so you don't want to run any user-native code on that process at all apart from something you can guarantee is near instant to avoid affecting the performance of the browser view etc.
So the basic approach should be to always IPC across to the PID_BROWSER and execute the native code there.

However I suppose that just because you're running on the browser process doesn't mean a long-running task can block it as the message loop would then stall anyway.

So you should look at it like this:
1) Near instant, you can get away with running it in the renderer process itself.
2) Fast enough to not impede the message loop processing, IPC it to the browser process and execute there, then IPC the result back to the renderer.
3) Long running, IPC to Browser process and spawn a new user thread to handle the native code, the user thread then once complete IPCs back to the renderer.

Does that sound correct ?
johnh
Techie
 
Posts: 44
Joined: Wed Aug 01, 2018 12:14 pm

Re: Browser freezes while executing native code

Postby Czarek » Mon Aug 13, 2018 5:42 am

Browser process main thread is UI thread. If you block UI thread you block UI. On Windows you can run a multi threaded message loop and in such case UI thread is no more main thread.
Maintainer of the CEF Python, PHP Desktop and CEF C API projects. My LinkedIn.
User avatar
Czarek
Virtuoso
 
Posts: 1927
Joined: Sun Nov 06, 2011 2:12 am

Re: Browser freezes while executing native code

Postby johnh » Mon Aug 13, 2018 8:39 am

Got it.

I've tried using the multi-threaded message loop mode, but haven't had any luck with it yet.. Given I'll be running this on Windows and other systems I guess it's easier to just go the thread route and keep the code the same across platforms.
johnh
Techie
 
Posts: 44
Joined: Wed Aug 01, 2018 12:14 pm

Re: Browser freezes while executing native code

Postby magreenblatt » Mon Aug 13, 2018 10:44 am

Don't perform blocking work on the CEF UI or IO threads. Create your own work thread or use one of the appropriate threads listed at https://bitbucket.org/chromiumembedded/ ... pes.h-1371
magreenblatt
Site Admin
 
Posts: 12383
Joined: Fri May 29, 2009 6:57 pm

Re: Browser freezes while executing native code

Postby johnh » Wed Aug 15, 2018 5:13 am

Got it :)

Thanks for the help!

One last question on this,
when you post the IPC from the Renderer to the Browser process is there a way to specify exactly which thread should be used at the same time?

IE:

browser->SendProcessMessage(PID_BROWSER, msg);

Or do you need to then use some other function once you receive the message in the browser process to specify which thread it should execute on?
johnh
Techie
 
Posts: 44
Joined: Wed Aug 01, 2018 12:14 pm

Re: Browser freezes while executing native code

Postby magreenblatt » Wed Aug 15, 2018 8:48 am

Once you receive the message you can post it to another thread using CefPostTask. See https://bitbucket.org/chromiumembedded/ ... ting-tasks
magreenblatt
Site Admin
 
Posts: 12383
Joined: Fri May 29, 2009 6:57 pm


Return to Support Forum

Who is online

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

cron