{"record":{"id":"ff86be9c22cc985d","repo":"cefsharp/CefSharp","slug":"ibrowserhost-is-null","errorCode":null,"errorMessage":"IBrowserHost is null","messagePattern":"IBrowserHost is null","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"CefSharp.Wpf.Example/Controls/ChromiumWebBrowserWithScreenshotSupport.cs","lineNumber":68,"sourceCode":"            {\n                throw new Exception(\"Screenshot already in progress, you must wait for the previous screenshot to complete\");\n            }\n\n            if (IsBrowserInitialized == false)\n            {\n                throw new Exception(\"Browser has not yet finished initializing or is being disposed\");\n            }\n\n            if (IsLoading)\n            {\n                throw new Exception(\"Unable to take screenshot while browser is loading\");\n            }\n\n            var browserHost = this.GetBrowserHost();\n\n            if (browserHost == null)\n            {\n                throw new Exception(\"IBrowserHost is null\");\n            }\n\n            screenshotTaskCompletionSource = new TaskCompletionSource<InteropBitmap>(TaskCreationOptions.RunContinuationsAsynchronously);\n\n            if (cancellationToken.HasValue)\n            {\n                var token = cancellationToken.Value;\n                cancellationTokenRegistration = token.Register(() =>\n                {\n                    screenshotTaskCompletionSource.TrySetCanceled();\n\n                    cancellationTokenRegistration?.Dispose();\n\n                }, useSynchronizationContext: false);\n            }\n\n            if (frameRate.HasValue)\n            {","sourceCodeStart":50,"sourceCodeEnd":86,"githubUrl":"https://github.com/cefsharp/CefSharp/blob/16bc6e0711a4945be17e8bc4bc7ccf9469f3fdec/CefSharp.Wpf.Example/Controls/ChromiumWebBrowserWithScreenshotSupport.cs#L50-L86","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before\nvar shot = browser.TakeScreenshot(size); // throws if host null race\n\n// after\nif (browser.GetBrowserHost() != null)\n    _ = browser.TakeScreenshot(size);\nelse\n    // defer and retry shortly","handlingStrategy":"validation","validationCode":"if (browser.GetBrowserHost() != null)\n{\n    _ = browser.TakeScreenshot(size);\n}","typeGuard":"static bool IsHostReady(ChromiumWebBrowserWithScreenshotSupport b) => b.GetBrowserHost() != null;","tryCatchPattern":null,"preventionTips":["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."],"tags":["cefwsharp","wpf","screenshot","lifecycle"],"backgroundTag":null,"analyzedSha":"16bc6e0711a4945be17e8bc4bc7ccf9469f3fdec","analyzedAt":"2026-08-13T19:57:05.828Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}