cefsharp/CefSharp · error · Exception

IBrowserHost instance is null. Browser has likely not finish

Error message

IBrowserHost instance is null. Browser has likely not finished initializing or is in the process of disposing.

What it means

ThrowExceptionIfBrowserHostNull fires when an extension method needs the IBrowserHost (e.g. GetCookieManager, show dev tools, print) but browser.GetBrowserHost() returns null. The host is the live CEF browser handle; a null host means the underlying browser process hasn't created the host yet or has already torn it down during disposal.

Source

Thrown at CefSharp/WebBrowserExtensions.cs:2000

        /// <param name="browser">The ChromiumWebBrowser instance this method extends.</param>
        public static void ThrowExceptionIfBrowserNull(IBrowser browser)
        {
            if (browser == null)
            {
                throw new Exception(BrowserNullExceptionString);
            }
        }

        /// <summary>
        /// Throw exception if browser host null.
        /// </summary>
        /// <exception cref="Exception">Thrown when an exception error condition occurs.</exception>
        /// <param name="browserHost">The browser host.</param>
        public static void ThrowExceptionIfBrowserHostNull(IBrowserHost browserHost)
        {
            if (browserHost == null)
            {
                throw new Exception(BrowserHostNullExceptionString);
            }
        }

        /// <summary>
        /// Throw exception if can execute javascript in main frame false.
        /// </summary>
        /// <exception cref="Exception">Thrown when an exception error condition occurs.</exception>
        public static void ThrowExceptionIfCanExecuteJavascriptInMainFrameFalse()
        {
            throw new Exception("Unable to execute javascript at this time, scripts can only be executed within a V8Context. " +
                                    "Use the IWebBrowser.CanExecuteJavascriptInMainFrame property to guard against this exception. " +
                                    "See https://github.com/cefsharp/CefSharp/wiki/General-Usage#when-can-i-start-executing-javascript " +
                                    "for more details on when you can execute javascript. For frames that do not contain Javascript then no " +
                                    "V8Context will be created. Executing a script once the frame has loaded it's possible to create a V8Context. " +
                                    "You can use browser.GetMainFrame().ExecuteJavaScriptAsync(script) or browser.GetMainFrame().EvaluateScriptAsync " +
                                    "to bypass these checks (advanced users only).");
        }
    }

View on GitHub (pinned to 16bc6e0711)

Solutions

  1. Wait until IsBrowserInitialized is true before invoking any host-dependent method.
  2. Null-check the host: var host = browser.GetBrowserHost(); if (host != null) { ... }.
  3. Avoid calling host methods from disposing handlers.
  4. For OffScreen, use the WaitForInitialization() pattern before touching the host.

Example fix

// before
var host = browser.GetBrowserHost();
host.ShowDevTools(); // host null -> throws

// after
if (browser.IsBrowserInitialized)
{
    var host = browser.GetBrowserHost();
    if (host != null)
    {
        host.ShowDevTools();
    }
}
Defensive patterns

Strategy: validation

Validate before calling

var host = browser.GetBrowserHost();
if (host != null)
{
    host.ShowDevTools();
}

Type guard

static bool HasBrowserHost(IWebBrowser browser) => browser?.GetBrowserHost() != null;

Prevention

When it happens

Trigger: Calling host-dependent extension methods when GetBrowserHost() returns null - before IsBrowserInitialized or during/after disposal. Common in OffScreen/WinForms hosts accessed synchronously right after construction.

Common situations: Calling GetCookieManager, ShowDevTools, PrintToPdfAsync, or similar immediately after new ChromiumWebBrowser() before the browser host exists. Accessing the host from a Form.Closing or disposed event handler.

Related errors


AI-assisted analysis of cefsharp/CefSharp@16bc6e0711 (2026-08-13). Data as JSON: /api/errors/539a02b21e85647e. Report an issue: GitHub.