cefsharp/CefSharp · error · Exception
IBrowserHost is null
Error message
IBrowserHost is null
What it means
Thrown by the example ChromiumWebBrowserWithScreenshotSupport.TakeScreenshot when this.GetBrowserHost() returns null after the earlier guards passed. The capture path needs IBrowserHost (to set WindowlessFrameRate and call WasResized), so a null host means the native host object is not available despite the browser reporting initialized. This is a defensive check for a narrow race or a disposing host.
Source
Thrown at CefSharp.Wpf.Example/Controls/ChromiumWebBrowserWithScreenshotSupport.cs:68
{
throw new Exception("Screenshot already in progress, you must wait for the previous screenshot to complete");
}
if (IsBrowserInitialized == false)
{
throw new Exception("Browser has not yet finished initializing or is being disposed");
}
if (IsLoading)
{
throw new Exception("Unable to take screenshot while browser is loading");
}
var browserHost = this.GetBrowserHost();
if (browserHost == null)
{
throw new Exception("IBrowserHost is null");
}
screenshotTaskCompletionSource = new TaskCompletionSource<InteropBitmap>(TaskCreationOptions.RunContinuationsAsynchronously);
if (cancellationToken.HasValue)
{
var token = cancellationToken.Value;
cancellationTokenRegistration = token.Register(() =>
{
screenshotTaskCompletionSource.TrySetCanceled();
cancellationTokenRegistration?.Dispose();
}, useSynchronizationContext: false);
}
if (frameRate.HasValue)
{View on GitHub (pinned to 16bc6e0711)
Solutions
- Guard with `if (browser.GetBrowserHost() != null)` and defer/retry the capture if null.
- Cancel captures when disposal begins so the host-null state is never hit.
- Add a short retry/backoff for the rare initialization race rather than throwing.
- If the host is persistently null, verify the browser is not in a disposing state before retrying.
Example fix
// before
var shot = browser.TakeScreenshot(size); // throws if host null race
// after
if (browser.GetBrowserHost() != null)
_ = browser.TakeScreenshot(size);
else
// defer and retry shortly Defensive patterns
Strategy: validation
Validate before calling
if (browser.GetBrowserHost() != null)
{
_ = browser.TakeScreenshot(size);
} Type guard
static bool IsHostReady(ChromiumWebBrowserWithScreenshotSupport b) => b.GetBrowserHost() != null;
Prevention
- Guard GetBrowserHost() before capturing to handle the initialization/dispose race.
- Cancel captures on dispose so the host-null state is not reached.
- Retry briefly if the host is transiently null.
When it happens
Trigger: The browser reports IsBrowserInitialized true and not loading, but the host object is momentarily null (initialization race) or being torn down; calling during the dispose sequence after BrowserCore is cleared.
Common situations: A race between the initialized flag and host availability; calling capture exactly as the control is disposed; edge cases in off-screen hosting where the host lags initialization.
Related errors
- Browser has not yet finished initializing or is being dispos
- Screenshot already in progress, you must wait for the previo
- Unable to take screenshot while browser is loading
- Browser has already been created. BrowserSettings must be se
- Browser has already been created. RequestContext must be set
AI-assisted analysis of cefsharp/CefSharp@16bc6e0711 (2026-08-13).
Data as JSON: /api/errors/ff86be9c22cc985d.
Report an issue: GitHub.