Search code examples
c#asynchronouscefsharp

EvaluateScriptAsync hanging


In CefSharp WinForms, I'm trying to get the html source of the page using JS once the page has loaded, however the application is freezing. I'm using a BackgroundWorker and the concerned functions are as follows:

void bw_DoWork(object sender, DoWorkEventArgs e)
{
    browser.Load("http://www.google.com");

    browser.FrameLoadEnd += delegate
    {
        object js = EvaluateScript(browser, "1+1");
        MessageBox.Show(js.ToString());
    };
}

object EvaluateScript(ChromiumWebBrowser b, string script)
{
    var task = b.EvaluateScriptAsync(script);
    task.Wait();
    return task.Result;
}

Solution

  • Whilst you assign FrameLoadEnd in the BackgroundWorker thread, it's actually executed on the underlying CEF UI thread, for which you cannot block without issues.

    I'd typically suggest you spawn a Task from within the event handler to complete your work.