From 0b8761ceb6044d41880ce5af744ac0c1719bb47d Mon Sep 17 00:00:00 2001 From: campersau Date: Sun, 22 Dec 2024 04:28:18 +0100 Subject: [PATCH] Avoid ObjectDisposedException when host got disposed (#5003) --- .../Partial/ChromiumWebBrowser.Partial.cs | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/CefSharp/Internals/Partial/ChromiumWebBrowser.Partial.cs b/CefSharp/Internals/Partial/ChromiumWebBrowser.Partial.cs index fe974d9a6..8458eebc8 100644 --- a/CefSharp/Internals/Partial/ChromiumWebBrowser.Partial.cs +++ b/CefSharp/Internals/Partial/ChromiumWebBrowser.Partial.cs @@ -449,11 +449,21 @@ private void InitialLoad(bool? isLoading, CefErrorCode? errorCode) initialLoadAction = null; - var host = browser?.GetHost(); + int statusCode = 0; - var navEntry = host?.GetVisibleNavigationEntry(); + try + { + var host = browser?.GetHost(); + + var navEntry = host?.GetVisibleNavigationEntry(); - int statusCode = navEntry?.HttpStatusCode ?? -1; + statusCode = navEntry?.HttpStatusCode ?? -1; + } + catch (ObjectDisposedException) + { + // The host might got disposed + // https://github.com/cefsharp/CefSharp/issues/5002 + } //By default 0 is some sort of error, we map that to -1 //so that it's clearer that something failed.