Cef笔记:Cef消息循环的集成
1. cef文档原文
https://bitbucket.org/chromiumembedded/cef/wiki/GeneralUsage
Message Loop Integration
CEF can also integrate with an existing application message loop instead of running its own message loop. There are two ways to do this.
Call CefDoMessageLoopWork() on a regular basis instead of calling CefRunMessageLoop(). Each call to CefDoMessageLoopWork() will perform a single iteration of the CEF message loop. Caution should be used with this approach. Calling the method too infrequently will starve the CEF message loop and negatively impact browser performance. Calling the method too frequently will negatively impact CPU usage. See CefBrowserProcessHandler::OnScheduleMessagePumpWork for advanced usage details. You can test this mode in cefclient by running with the “–external-message-pump” command-line flag.
Set CefSettings.multi_threaded_message_loop = true (Windows and Linux only). This will cause CEF to run the browser UI thread on a separate thread from the main application thread. With this approach neither CefDoMessageLoopWork() nor CefRunMessageLoop() need to be called. CefInitialize() and CefShutdown() should still be called on the main application thread. You will need to provide your own mechanism for communicating with the main application thread (see for example the message window usage in cefclient_win.cpp). You can test this mode in cefclient on Windows or Linux by running with the “–multi-threaded-message-loop” command-line flag.
2. 译文
CEF 还可以与现有应用程序的消息循环集成,而非运行自身的消息循环。实现这一目标有两种方式:
定期调用 CefDoMessageLoopWork() 替代 CefRunMessageLoop()
每次调用 CefDoMessageLoopWork() 会执行一次 CEF 消息循环的单次迭代。使用此方法时需谨慎:调用频率过低会导致 CEF 消息循环“饥饿”,进而降低浏览器性能;调用频率过高则会增加 CPU 占用。高级用法详见 CefBrowserProcessHandler::OnScheduleMessagePumpWork。
在 cefclient 中,可通过命令行参数 --external-message-pump 测试此模式。
设置 CefSettings.multi_threaded_message_loop = true(仅限 Windows 和 Linux)
此设置会使 CEF 在独立于主应用程序线程的线程中运行浏览器 UI 线程。采用此方式时,无需调用 CefDoMessageLoopWork() 或 CefRunMessageLoop(),但仍需在主应用程序线程中调用 CefInitialize() 和 CefShutdown()。
您需要自行实现与主应用程序线程通信的机制(例如,可参考 cefclient_win.cpp 中消息窗口的使用方式)。在 Windows 或 Linux 的 cefclient 中,可通过命令行参数 --multi-threaded-message-loop 测试此模式。