ClientApp constructor runs several times?

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.

ClientApp constructor runs several times?

Postby payalord » Sun Jun 17, 2018 5:09 am

Here is what I have:

Code: Select all
class ClientApp : public CefApp, public CefRenderProcessHandler, public CefBrowserProcessHandler {
public:
    ClientApp();
   ~ClientApp();
// ...


Code: Select all
ClientApp::ClientApp()
{
   Environment::Initialize();
}

ClientApp::~ClientApp()
{
   Environment::ShutDown();
}

void ClientApp::OnWebKitInitialized()
{
// ...


Code: Select all
class ClientHandler : public CefClient, public CefLifeSpanHandler {
public:
    ClientHandler();
// ...
virtual bool OnProcessMessageReceived(CefRefPtr<CefBrowser> browser, CefProcessId source_process, CefRefPtr<CefProcessMessage> message) OVERRIDE;
// ...


So I just noticed that the code in my Environment::Initialize() runs every time after OnProcessMessageReceived finishes it's work. Is it normal? While I'm using with this particular ClientHandler and ClientApp only 1 browser.
payalord
Techie
 
Posts: 21
Joined: Sun Jan 07, 2018 8:21 am

Re: ClientApp constructor runs several times?

Postby amaitland » Sun Jun 17, 2018 7:14 am

What does your GetBrowserProcessHandler implementation look like?
Maintainer of the CefSharp project.
amaitland
Virtuoso
 
Posts: 1292
Joined: Wed Jan 14, 2015 2:35 am

Re: ClientApp constructor runs several times?

Postby payalord » Sun Jun 17, 2018 5:17 pm

Code: Select all
   CefRefPtr<CefBrowserProcessHandler> GetBrowserProcessHandler() OVERRIDE
   {
      return this;
   }


But i found that it is not related to ClientApp object. I moved Environment::Initialize(); from ClientApp into main.cpp to WinMain function like this:
Code: Select all
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE, LPSTR, int nCmdShow)
{
    Environment::Initialize();
    // ... some code
    CefShutDown();
    return result;
}


And creating new browser window with different client handler called RequestClientHandler inside ClientHandler::OnProcessMessageReceived with CefBrowserHost::CreateBrowserSync function causing that the code of Environment::Initialize() running again. There is no other places other than first line of WinMain where this code must run. But how it can run twice then?

And yeah browser with CefBrowserHost::CreateBrowserSync and working fine. But i don't understand why WinMain runs again and how?

Is it possible that because new process/thread is created for new browser, that it runs separately WinMain again for itself by some reason?
payalord
Techie
 
Posts: 21
Joined: Sun Jan 07, 2018 8:21 am

Re: ClientApp constructor runs several times?

Postby amaitland » Sun Jun 17, 2018 5:37 pm

Are you properly handling the sub process?

https://bitbucket.org/chromiumembedded/ ... t-function
Maintainer of the CefSharp project.
amaitland
Virtuoso
 
Posts: 1292
Joined: Wed Jan 14, 2015 2:35 am

Re: ClientApp constructor runs several times?

Postby payalord » Mon Jun 18, 2018 2:12 am

I'm using single executable and this is my main function:
Code: Select all
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE, LPSTR, int nCmdShow)
{
   Environment::Initialize();
   CefMainArgs main_args(hInstance);

    CefRefPtr<ClientApp> app(new ClientApp);

    // Execute the secondary process, if any.
    int exit_code = CefExecuteProcess( main_args, app.get(), NULL );
    if ( exit_code >= 0 ) {
        exit(exit_code);
    }

    // Register the window class.
    HWND hwnd = RegisterWindow(hInstance, nCmdShow);
    if ( hwnd == 0 ) {
        return 0;
    }

   tsks.SetParent(hwnd);

    RECT rect;
    GetClientRect(hwnd, &rect);

    CefSettings settings;
   settings.remote_debugging_port = 8080;
    CefInitialize( main_args, settings, app.get(), NULL );
    CefWindowInfo        info;
    CefBrowserSettings   b_settings;
    CefRefPtr<CefClient> client(new ClientHandler);
    g_handler = (ClientHandler *) client.get();
    //std::string path = "file://" + GetApplicationDir() + "/html/index.html";
   std::string path = "resource://html/index.html";
    CefRefPtr<CefCommandLine> command_line = CefCommandLine::GetGlobalCommandLine();

    if ( command_line->HasSwitch("url") ) {
        path = command_line->GetSwitchValue("url");
    }

    info.SetAsChild(hwnd, rect);
    CefBrowserHost::CreateBrowser(info, client.get(), path, b_settings, NULL);
    int result = 0;

    if ( !settings.multi_threaded_message_loop ) {
        // Run the CEF message loop. This function will block until the application
        // recieves a WM_QUIT message.
        CefRunMessageLoop();
    } else {
        // Create a hidden window for message processing.
        HWND hMessageWnd = CreateMessageWindow(hInstance);
        MSG  msg;

        // Run the application message loop.
        while ( GetMessage(&msg, NULL, 0, 0) ) {
         CefDoMessageLoopWork();
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }

        DestroyWindow(hMessageWnd);
        hMessageWnd = NULL;
        result = static_cast<int> (msg.wParam);
    }

    CefShutdown();
   tsks.StopTasks();
   Environment::ShutDown();
    return result;
}
payalord
Techie
 
Posts: 21
Joined: Sun Jan 07, 2018 8:21 am


Return to Support Forum

Who is online

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