{"record":{"id":"766b44cdfea80294","repo":"cefsharp/CefSharp","slug":"this-method-can-only-be-called-on-the-cef-ui-threa","errorCode":null,"errorMessage":"This method can only be called on the CEF UI thread.Use Cef.UIThreadTaskFactory to marshal your call onto the CEF UI Thread.","messagePattern":"This method can only be called on the CEF UI thread\\.Use Cef\\.UIThreadTaskFactory to marshal your call onto the CEF UI Thread\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"CefSharp.WinForms/WebBrowserExtensions.cs","lineNumber":41,"sourceCode":"        /// This will avoid the WM_Close message that CEF sends by default to the top level window.\n        /// (Which closes your application). This method should generally only be used in the WinForms version.\n        /// </summary>\n        /// <param name=\"chromiumWebBrowser\">the <see cref=\"ChromiumWebBrowser\"/> or <see cref=\"ChromiumHostControl\"/> instance.</param>\n        /// <returns>If the function succeeds, the return value is true.</returns>\n        /// <example>\n        /// <code>\n        /// //Invoke on the CEF UI Thread\n        /// Cef.UIThreadTaskFactory.StartNew(() =>\n        /// {\n        ///   var closed = chromiumWebBrowser.DestroyWindow();\n        /// });\n        /// </code>\n        /// </example>\n        public static bool DestroyWindow(this IChromiumWebBrowserBase chromiumWebBrowser)\n        {\n            if (!Cef.CurrentlyOnThread(CefThreadIds.TID_UI))\n            {\n                throw new InvalidOperationException(\"This method can only be called on the CEF UI thread.\" +\n                    \"Use Cef.UIThreadTaskFactory to marshal your call onto the CEF UI Thread.\");\n            }\n\n            if (chromiumWebBrowser.IsDisposed)\n            {\n                return false;\n            }\n\n            var browser = chromiumWebBrowser.BrowserCore;\n\n            if (browser == null)\n            {\n                return false;\n            }\n\n            var handle = browser.GetHost().GetWindowHandle();\n\n","sourceCodeStart":23,"sourceCodeEnd":59,"githubUrl":"https://github.com/cefsharp/CefSharp/blob/16bc6e0711a4945be17e8bc4bc7ccf9469f3fdec/CefSharp.WinForms/WebBrowserExtensions.cs#L23-L59","documentation":"Thrown by the DestroyWindow extension method when the calling thread is not the CEF UI thread (CefThreadIds.TID_UI). DestroyWindow P/Invokes the native user32 DestroyWindow against the browser HWND; Win32 requires this be called on the thread that owns the window, which for CEF is the UI thread. Calling from any other thread would silently misbehave or deadlock, so CefSharp enforces the thread affinity explicitly.","triggerScenarios":"Calling chromiumWebBrowser.DestroyWindow() directly from a background task, an async continuation, a non-UI event handler, or the main WinForms/WPF UI thread when CEF runs its UI thread separately.","commonSituations":"Attempting to forcibly close the browser from a Task.Run or a Dispose handler running on a worker thread; mixing the application UI thread with the CEF UI thread.","solutions":["Marshal the call onto the CEF UI thread via `Cef.UIThreadTaskFactory.StartNew(() => browser.DestroyWindow())`.","Confirm with `Cef.CurrentlyOnThread(CefThreadIds.TID_UI)` before invoking if unsure.","Prefer the higher-level browser close APIs (CloseBrowser) unless you specifically need the raw Win32 DestroyWindow semantics.","Do not call from async void event handlers that may resume on a thread-pool thread."],"exampleFix":"// before\nbrowser.DestroyWindow(); // throws: not on CEF UI thread\n\n// after\nCef.UIThreadTaskFactory.StartNew(() => browser.DestroyWindow());","handlingStrategy":"validation","validationCode":"if (Cef.CurrentlyOnThread(CefThreadIds.TID_UI))\n{\n    browser.DestroyWindow();\n}\nelse\n{\n    Cef.UIThreadTaskFactory.StartNew(() => browser.DestroyWindow());\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Always wrap DestroyWindow in Cef.UIThreadTaskFactory.StartNew unless you are certain you are on the CEF UI thread.","Check Cef.CurrentlyOnThread(CefThreadIds.TID_UI) when in doubt.","Prefer higher-level CloseBrowser APIs unless you need raw Win32 semantics."],"tags":["cefwsharp","winforms","threading","interop"],"backgroundTag":null,"analyzedSha":"16bc6e0711a4945be17e8bc4bc7ccf9469f3fdec","analyzedAt":"2026-08-13T19:57:05.828Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}