{"record":{"id":"89f48d2908cefd21","repo":"cefsharp/CefSharp","slug":"screenshot-already-in-progress-you-must-wait-for","errorCode":null,"errorMessage":"Screenshot already in progress, you must wait for the previous screenshot to complete","messagePattern":"Screenshot already in progress, you must wait for the previous screenshot to complete","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"CefSharp.Wpf.Example/Controls/ChromiumWebBrowserWithScreenshotSupport.cs","lineNumber":51,"sourceCode":"        private volatile bool isTakingScreenshot = false;\n        private Size? screenshotSize;\n        private int oldFrameRate;\n        private int ignoreFrames = 0;\n        private TaskCompletionSource<InteropBitmap> screenshotTaskCompletionSource;\n        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            }","sourceCodeStart":33,"sourceCodeEnd":69,"githubUrl":"https://github.com/cefsharp/CefSharp/blob/16bc6e0711a4945be17e8bc4bc7ccf9469f3fdec/CefSharp.Wpf.Example/Controls/ChromiumWebBrowserWithScreenshotSupport.cs#L33-L69","documentation":"Thrown by the example ChromiumWebBrowserWithScreenshotSupport.TakeScreenshot when a previous screenshot Task is still Running (tracked via a TaskCompletionSource whose Task.Status == TaskStatus.Running). The control resizes the off-screen browser to capture a frame and cannot run two captures concurrently without corrupting the frame buffer and state, so it serializes screenshots.","triggerScenarios":"Invoking TakeScreenshot again before the prior returned Task has completed; firing the ScreenshotCommand rapidly; calling from a loop without awaiting.","commonSituations":"A UI button with no debounce; chaining screenshots without await; a timer firing faster than capture completes.","solutions":["Await the Task returned by the previous TakeScreenshot call before starting another.","Track an `isCapturing` flag (or use the TCS) and ignore/disable the trigger while a capture is in flight.","Debounce the ScreenshotCommand or button so rapid clicks do not stack calls.","Serialize captures in a queue if you genuinely need back-to-back screenshots."],"exampleFix":"// before\nvar shot1 = browser.TakeScreenshot(size);\nvar shot2 = browser.TakeScreenshot(size); // throws: previous still running\n\n// after\nvar shot1 = await browser.TakeScreenshot(size);\nvar shot2 = await browser.TakeScreenshot(size);","handlingStrategy":"validation","validationCode":"if (screenshotTcs == null || screenshotTcs.Task.Status != TaskStatus.Running)\n{\n    screenshotTcs = browser.TakeScreenshot(size);\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Always await the previous TakeScreenshot Task before starting another.","Debounce the screenshot trigger / command.","Track an isCapturing flag and disable the UI button while in flight."],"tags":["cefwsharp","wpf","screenshot","concurrency"],"backgroundTag":null,"analyzedSha":"16bc6e0711a4945be17e8bc4bc7ccf9469f3fdec","analyzedAt":"2026-08-13T19:57:05.828Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}