{"record":{"id":"726f626e7e92f7d3","repo":"cefsharp/CefSharp","slug":"ibrowser-instance-is-no-longer-valid-control-hand","errorCode":null,"errorMessage":"IBrowser instance is no longer valid. Control.Handle was likely destroyed.","messagePattern":"IBrowser instance is no longer valid\\. Control\\.Handle was likely destroyed\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"CefSharp.WinForms/ChromiumWebBrowser.cs","lineNumber":477,"sourceCode":"            var browserCore = BrowserCore;\n\n            //There's a small window here between CreateBrowser\n            //and OnAfterBrowserCreated where the Address prop\n            //will be updated, no MainFrame.LoadUrl call will be made.\n            if (browserCore == null)\n            {\n                Address = url;\n            }\n            else\n            {\n                if(browserCore.IsDisposed)\n                {\n                    return;\n                }\n\n                if(!browserCore.IsValid)\n                {\n                    throw new InvalidOperationException(\"IBrowser instance is no longer valid. Control.Handle was likely destroyed.\");\n                }\n\n                using (var frame = browserCore.MainFrame)\n                {\n                    //Only attempt to call load if frame is valid\n                    //I've seen so far one case where the MainFrame is invalid.\n                    //As yet unable to reproduce\n                    if (frame.IsValid)\n                    {\n                        frame.LoadUrl(url);\n                    }\n                }\n            }\n        }\n\n        /// <summary>\n        /// Capture page screenshot.\n        /// </summary>","sourceCodeStart":459,"sourceCodeEnd":495,"githubUrl":"https://github.com/cefsharp/CefSharp/blob/16bc6e0711a4945be17e8bc4bc7ccf9469f3fdec/CefSharp.WinForms/ChromiumWebBrowser.cs#L459-L495","documentation":"Thrown by ChromiumWebBrowser.Load(string) when the IBrowserCore exists but IsValid is false, meaning the underlying CEF browser object has been torn down while the .NET wrapper still references it. The control handle was destroyed (form closing, tab disposed, reparenting) so navigation is impossible. Load guards disposal first (returns silently) but treats an invalid-but-not-disposed core as an error.","triggerScenarios":"Calling Load(url) while or after the control's handle is being destroyed: during Form.FormClosing, after removing the control from its parent, during tab teardown, or racing with asynchronous disposal.","commonSituations":"Navigating from a closing event handler; a background task calling Load after the user closed the tab/window; rapid open/close of browser instances; reparenting that recreates the handle.","solutions":["Guard Load calls with `if (!browser.IsDisposed && browser.BrowserCore?.IsValid == true)` before navigating.","Cancel background navigation tasks (CancellationToken) when the form/tab closes.","Prefer the async `LoadUrlAsync` extension which handles lifecycle more gracefully, and await it with a cancellation token.","Avoid calling Load from FormClosing/Dispose paths; queue the navigation or skip it if the handle is gone."],"exampleFix":"// before\nbrowser.Load(nextUrl); // throws if handle destroyed mid-session\n\n// after\nif (!browser.IsDisposed && browser.BrowserCore is { } core && core.IsValid)\n{\n    browser.Load(nextUrl);\n}","handlingStrategy":"validation","validationCode":"if (!browser.IsDisposed && browser.BrowserCore is { } core && core.IsValid)\n{\n    browser.Load(url);\n}","typeGuard":null,"tryCatchPattern":"try { browser.Load(url); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"no longer valid\"))\n{\n    // handle/control destroyed mid-navigation; abort gracefully\n}","preventionTips":["Cancel background navigation tasks when the tab/window closes.","Prefer LoadUrlAsync with a CancellationToken over fire-and-forget Load.","Never call Load from FormClosing/Dispose paths."],"tags":["cefwsharp","winforms","lifecycle","navigation"],"backgroundTag":null,"analyzedSha":"16bc6e0711a4945be17e8bc4bc7ccf9469f3fdec","analyzedAt":"2026-08-13T19:57:05.828Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}