Perform a task on a specified CEF thread

Source: Internet
Author: User

CEF has several threads, such as tid_ui, such as Tid_render, each of which has a specific meaning, and the specific category is defined in Cef_types.h, with the following excerpt:

  typedef enum {//BROWSER process THREADS-only available in the BROWSER process. The main thread in the browser. This would be the same as the main//application thread if Cefinitialize () are called with A//Cefsettings.multi_threade  D_message_loop value of FALSE.  TID_UI,////used to interact with the database.  tid_db,////used to interact with the file system.  Tid_file,////used for the FILE system operations that block user interactions.  Responsiveness of this thread affects users.  Tid_file_user_blocking,////used to launch and terminate browser processes.  Tid_process_launcher,////used to handle slow HTTP cache operations.  Tid_cache,/////used to process IPC and network messages.  tid_io,//Render Process THREADS-only available in the render process. The main thread in the renderer.  Used for all WebKit and V8 interaction. Tid_renderer,} cef_thread_id_t;  

For descriptions of these threads, see the comments in the code above, and you can also join here: https://bitbucket.org/chromiumembedded/cef/wiki/GeneralUsage#markdown-header-threads.

If we want to perform a task on a thread, such as when you want to pop up a window in the browser process, execute your code on the TID_UI thread. Similar tasks can be done through ceftask. Very simply, Cef_task.h has a ceftask interface declaration, we inherit it, and then use the Cefposttask.

The code resembles the following:

#include "include/cef_task.h"void doSomethingOnUIThread(){    MessageBox(...);}class SomeTask : public CefTask{    IMPLEMENT_REFCOUNTING(SomeTask);public:    void Execute()    {        doSomethingOnUIThread();    }};void doSomething(){    if (CefCurrentlyOn(TID_UI))    {        doSomethingOnUIThread();    }    else    {        CefRefPtr<SomeTask> t = new SomeTask;        CefPostTask(TID_UI, t);    }}

If you do not want to write a class to implement the Ceftask interface, CEF has some assistive means to help you. Let's look at the following code snippet:

if (!CefCurrentlyOn(TID_UI)) {    // Execute on the UI thread.    CefPostTask(TID_UI,        base::Bind(&ClientHandler::CloseAllBrowsers, this, force_close));    return;}

This is cefsimple, it uses base::bind to generate a functor, using a similar STL-like bind, including the "Include/base/cef_bind.h" header file.

That's it.

Other reference articles are described in my column: "CEF and Ppapi development ".

Perform a task on a specified CEF thread

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.