{"record":{"id":"27a470d1c37aad21","repo":"cefsharp/CefSharp","slug":"browser-has-not-yet-finished-initializing-or-is-be","errorCode":null,"errorMessage":"Browser has not yet finished initializing or is being disposed","messagePattern":"Browser has not yet finished initializing or is being disposed","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"CefSharp.Wpf.Example/Controls/ChromiumWebBrowserWithScreenshotSupport.cs","lineNumber":56,"sourceCode":"        private CancellationTokenRegistration? cancellationTokenRegistration;\n\n        public ICommand ScreenshotCommand { get; set; }\n\n        public ChromiumWebBrowserWithScreenshotSupport() : base()\n        {\n            ScreenshotCommand = new RelayCommand(TakeScreenshot);\n        }\n\n        public Task<InteropBitmap> TakeScreenshot(Size screenshotSize, int? frameRate = 1, int? ignoreFrames = 0, CancellationToken? cancellationToken = null)\n        {\n            if (screenshotTaskCompletionSource != null && screenshotTaskCompletionSource.Task.Status == TaskStatus.Running)\n            {\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            {","sourceCodeStart":38,"sourceCodeEnd":74,"githubUrl":"https://github.com/cefsharp/CefSharp/blob/16bc6e0711a4945be17e8bc4bc7ccf9469f3fdec/CefSharp.Wpf.Example/Controls/ChromiumWebBrowserWithScreenshotSupport.cs#L38-L74","documentation":"Thrown by the example ChromiumWebBrowserWithScreenshotSupport.TakeScreenshot when IsBrowserInitialized is false. Capture works by resizing the off-screen browser and reading the rendered frame in OnPaint; if the browser has not finished initializing (or is being disposed) there is no renderer to produce a frame, so capture is rejected up front.","triggerScenarios":"Calling TakeScreenshot before the browser finished its initial creation; calling during shutdown while the browser is disposing.","commonSituations":"Wiring the screenshot to a window Loaded event that fires before CEF reports IsBrowserInitialized; capturing immediately after setting Address; capturing during app exit.","solutions":["Wait until IsBrowserInitialized is true (subscribe to IsBrowserInitializedChanged or await initialization) before calling TakeScreenshot.","Guard with `if (browser.IsBrowserInitialized)` and disable the capture command until initialized.","Cancel any pending capture when the control starts disposing.","Ensure the browser has a loaded/committed page before capturing."],"exampleFix":"// before\npublic MainWindow()\n{\n    browser = new ChromiumWebBrowserWithScreenshotSupport();\n    var shot = browser.TakeScreenshot(size); // throws: not initialized\n}\n\n// after\nbrowser.IsBrowserInitializedChanged += (s, e) =>\n{\n    if (browser.IsBrowserInitialized)\n        _ = browser.TakeScreenshot(size);\n};","handlingStrategy":"validation","validationCode":"if (browser.IsBrowserInitialized)\n{\n    _ = browser.TakeScreenshot(size);\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Subscribe to IsBrowserInitializedChanged and capture only after it flips true.","Disable the screenshot command until initialized.","Cancel pending captures when disposal begins."],"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"}