Scheme Handler freeze the UI

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.

Scheme Handler freeze the UI

Postby abizeau » Mon Mar 12, 2018 5:21 pm

Hello,

I have an image processing call (about 5 to 10 second) inside a ProcessRequest() and my XHR is async, but the UI still freeze.

Is there any thing to do such as using callback function or use threading ?

Regards,
Alex
abizeau
Techie
 
Posts: 34
Joined: Thu Feb 15, 2018 7:07 pm

Re: Scheme Handler freeze the UI

Postby magreenblatt » Mon Mar 12, 2018 5:25 pm

Do not perform blocking work on the IO thread (inside ProcessRequest). Do the work on a different thread and use the async callback to continue when done.
magreenblatt
Site Admin
 
Posts: 12408
Joined: Fri May 29, 2009 6:57 pm

Re: Scheme Handler freeze the UI

Postby abizeau » Mon Mar 12, 2018 9:02 pm

When you means another thread, a std::thread or Renderer thread ?

Do you have any example or topics to link ? I search a bit and I found nothing about it.
abizeau
Techie
 
Posts: 34
Joined: Thu Feb 15, 2018 7:07 pm

Re: Scheme Handler freeze the UI

Postby magreenblatt » Mon Mar 12, 2018 9:49 pm

You can create the new thread however you like. CEF also provides a CefThread class.
magreenblatt
Site Admin
 
Posts: 12408
Joined: Fri May 29, 2009 6:57 pm

Re: Scheme Handler freeze the UI

Postby abizeau » Tue Mar 13, 2018 4:21 pm

Just to inform anyone who search for something like this, here an example:

Code: Select all
bool ProcessRequest(CefRefPtr<CefRequest> request, CefRefPtr<CefCallback> callback)
{
    if ( strstr(url.c_str(), "myaction") != NULL )
    {
        std::async(&RunLongProcess, this, request->GetPostData(), callback);
        return true; // Handled request;
    }

    // Dont put the Callback->Continue() here, reserve it for your thread.
    return false;
}


void RunLongProcess(CefRefPtr<CefPostData> postData, CefRefPtr<CefCallback> callback)
{
    // Check if postData has been provided.
    if ( postData == NULL )   
        Callback->Cancel(); // Cancel the request.

   // processing ...

    // Similar to other ProcessRequest set the m_data and your m_mime_type (if you have one).

    // Then send to the factory, that the request is finished to process
    // and it can now call the GetResponseHeaders and ReadResponse.
    Callback->Continue();
}

Last edited by abizeau on Thu Mar 15, 2018 10:07 am, edited 1 time in total.
abizeau
Techie
 
Posts: 34
Joined: Thu Feb 15, 2018 7:07 pm

Re: Scheme Handler freeze the UI

Postby abizeau » Thu Mar 15, 2018 10:07 am

I discover that std::async(); have a different behavior on Mac OS. To be able to return true, you might want to use std::thread().detach(); instead. Else the thread is block after the return, to wait for the dtor to finish, which wait for the async thread.

So the solution in the
Code: Select all
bool ProcessRequest(CefRefPtr<CefRequest> request, CefRefPtr<CefCallback> callback)
{
    if ( strstr(url.c_str(), "myaction") != NULL )
    {
        std::thread(&RunLongProcess, this, request->GetPostData(), callback)­.detach();
        return true; // Handled request;
    }

    // Dont put the Callback->Continue() here, reserve it for your thread.
    return false;
}
abizeau
Techie
 
Posts: 34
Joined: Thu Feb 15, 2018 7:07 pm


Return to Support Forum

Who is online

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