{"record":{"id":"ed688caf35b69c68","repo":"cefsharp/CefSharp","slug":"the-chromiumwebbrowser-instance-creates-the-underl","errorCode":null,"errorMessage":"The ChromiumWebBrowser instance creates the underlying Chromium Embedded Framework (CEF) browser instance in an async fashion. The undelying CefBrowser instance is not yet initialized. Use the IsBrowserInitializedChanged event and check the IsBrowserInitialized property to determine when the browser has been initialized.","messagePattern":"The ChromiumWebBrowser instance creates the underlying Chromium Embedded Framework \\(CEF\\) browser instance in an async fashion\\. The undelying CefBrowser instance is not yet initialized\\. Use the IsBrowserInitializedChanged event and check the IsBrowserInitialized property to determine when the browser has been initialized\\.","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"CefSharp.Wpf/HwndHost/ChromiumWebBrowser.cs","lineNumber":1939,"sourceCode":"            }\n\n            using (var devToolsClient = browser.GetDevToolsClient())\n            {\n                var screenShot = await devToolsClient.Page.CaptureScreenshotAsync(format, quality, viewPort, fromSurface, captureBeyondViewport).ConfigureAwait(continueOnCapturedContext: false);\n\n                return screenShot.Data;\n            }\n        }\n\n        /// <summary>\n        /// Throw exception if browser not initialized.\n        /// </summary>\n        /// <exception cref=\"Exception\">Thrown when an exception error condition occurs.</exception>\n        private void ThrowExceptionIfBrowserNotInitialized()\n        {\n            if (!InternalIsBrowserInitialized())\n            {\n                throw new Exception(BrowserNotInitializedExceptionErrorMessage);\n            }\n        }\n\n        /// <summary>\n        /// Throw exception if disposed.\n        /// </summary>\n        /// <exception cref=\"ObjectDisposedException\">Thrown when a supplied object has been disposed.</exception>\n        private void ThrowExceptionIfDisposed()\n        {\n            if (IsDisposed)\n            {\n                throw new ObjectDisposedException(\"browser\", \"Browser has been disposed\");\n            }\n        }\n\n        private int GetChromiumChildProcessId(string frameIdentifier)\n        {\n            try","sourceCodeStart":1921,"sourceCodeEnd":1957,"githubUrl":"https://github.com/cefsharp/CefSharp/blob/16bc6e0711a4945be17e8bc4bc7ccf9469f3fdec/CefSharp.Wpf/HwndHost/ChromiumWebBrowser.cs#L1921-L1957","documentation":"The underlying CefBrowser instance is created asynchronously after the ChromiumWebBrowser control is constructed. Any API that needs the live browser (screenshots, dev tools, script execution, navigation queries) calls ThrowExceptionIfBrowserNotInitialized, which checks an interlocked flag set only after CEF reports initialization complete.","triggerScenarios":"Calling browser-dependent methods — CaptureScreenshotAsync, GetBrowser, EvaluateScriptAsync (on the HwndHost variant), PrintToPdfAsync, etc. — before IsBrowserInitialized is true.","commonSituations":"Trying to load a URL or take a screenshot immediately after `new ChromiumWebBrowser()`. Accessing the browser in a constructor or early Loaded handler without awaiting initialization.","solutions":["Subscribe to IsBrowserInitializedChanged and check IsBrowserInitialized before calling browser-dependent APIs.","Await the browser's initialization task (LoadUrlAsync or similar) before invoking further operations.","Guard every call with: if (browser.IsBrowserInitialized) { ... } else { /* wait or queue */ }."],"exampleFix":"// before — throws because browser is not yet initialized\nvar browser = new ChromiumWebBrowser();\nvar shot = await browser.CaptureScreenshotAsync();\n\n// after — wait for initialization\nbrowser.IsBrowserInitializedChanged += async (s, e) =>\n{\n    if (browser.IsBrowserInitialized)\n    {\n        var shot = await browser.CaptureScreenshotAsync();\n    }\n};","handlingStrategy":"validation","validationCode":"if (!browser.IsBrowserInitialized)\n{\n    throw new InvalidOperationException(\n        \"Browser not initialized. Await IsBrowserInitializedChanged before calling this API.\");\n}\n// safe to call browser-dependent API now","typeGuard":null,"tryCatchPattern":"try\n{\n    var shot = await browser.CaptureScreenshotAsync();\n}\ncatch (Exception ex) when (ex.Message.Contains(\"not yet initialized\"))\n{\n    // Queue the operation for after initialization\n    browser.IsBrowserInitializedChanged += async (s, e) =>\n    {\n        if (browser.IsBrowserInitialized)\n            await browser.CaptureScreenshotAsync();\n    };\n}","preventionTips":["Always subscribe to IsBrowserInitializedChanged before calling browser APIs.","Gate browser-dependent calls behind an IsBrowserInitialized check.","Use async load methods (LoadUrlAsync) that await initialization internally."],"tags":["browser-initialization","lifecycle","async","wpf-hwndhost"],"backgroundTag":null,"analyzedSha":"16bc6e0711a4945be17e8bc4bc7ccf9469f3fdec","analyzedAt":"2026-08-13T19:57:05.828Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}