diff --git a/CefSharp.Core.Runtime/ManagedCefBrowserAdapter.cpp b/CefSharp.Core.Runtime/ManagedCefBrowserAdapter.cpp
index 545ece401..272266da4 100644
--- a/CefSharp.Core.Runtime/ManagedCefBrowserAdapter.cpp
+++ b/CefSharp.Core.Runtime/ManagedCefBrowserAdapter.cpp
@@ -105,19 +105,28 @@ namespace CefSharp
// NOTE: This was moved out of OnAfterBrowserCreated to prevent the System.ServiceModel assembly from being loaded when WCF is not enabled.
__declspec(noinline) void ManagedCefBrowserAdapter::InitializeBrowserProcessServiceHost(IBrowser^ browser)
{
- _browserProcessServiceHost = gcnew BrowserProcessServiceHost(_javaScriptObjectRepository, Process::GetCurrentProcess()->Id, browser->Identifier, _javascriptCallbackFactory);
- //NOTE: Attempt to solve timing issue where browser is opened and rapidly disposed. In some cases a call to Open throws
- // an exception about the process already being closed. Two relevant issues are #862 and #804.
- if (_browserProcessServiceHost->State == CommunicationState::Created)
+ if (browser->IsDisposed)
{
- try
+ return;
+ }
+
+ //As the default the browser is Disposed on a different thread to this method it's possible that
+ //we arrive here after browser has started Disposing. In that case there's likely to be an exception
+ //accessing the Browser and possibly the serviceHost.
+ //https://github.com/cefsharp/CefSharp/issues/3437
+ try
+ {
+ _browserProcessServiceHost = gcnew BrowserProcessServiceHost(_javaScriptObjectRepository, Process::GetCurrentProcess()->Id, browser->Identifier, _javascriptCallbackFactory);
+ //NOTE: Attempt to solve timing issue where browser is opened and rapidly disposed. In some cases a call to Open throws
+ // an exception about the process already being closed. Two relevant issues are #862 and #804.
+ if (_browserProcessServiceHost->State == CommunicationState::Created)
{
_browserProcessServiceHost->Open();
- }
- catch (Exception^)
- {
- //Ignore exception as it's likely cause when the browser is closing
- }
+ }
+ }
+ catch (Exception^)
+ {
+ //Ignore exception as it's likely cause when the browser is closing/disposed
}
}
diff --git a/CefSharp.OffScreen/ChromiumWebBrowser.cs b/CefSharp.OffScreen/ChromiumWebBrowser.cs
index b0bccb2ef..755f6db96 100644
--- a/CefSharp.OffScreen/ChromiumWebBrowser.cs
+++ b/CefSharp.OffScreen/ChromiumWebBrowser.cs
@@ -663,6 +663,11 @@ namespace CefSharp.OffScreen
/// The browser.
void IWebBrowserInternal.OnAfterBrowserCreated(IBrowser browser)
{
+ if (IsDisposed || browser.IsDisposed)
+ {
+ return;
+ }
+
this.browser = browser;
Interlocked.Exchange(ref browserInitialized, 1);
diff --git a/CefSharp.WinForms/ChromiumWebBrowser.cs b/CefSharp.WinForms/ChromiumWebBrowser.cs
index 9f82401e3..ed4f3e555 100644
--- a/CefSharp.WinForms/ChromiumWebBrowser.cs
+++ b/CefSharp.WinForms/ChromiumWebBrowser.cs
@@ -607,6 +607,11 @@ namespace CefSharp.WinForms
/// The browser.
void IWebBrowserInternal.OnAfterBrowserCreated(IBrowser browser)
{
+ if(IsDisposed || browser.IsDisposed)
+ {
+ return;
+ }
+
this.browser = browser;
Interlocked.Exchange(ref browserInitialized, 1);
diff --git a/CefSharp.Wpf/ChromiumWebBrowser.cs b/CefSharp.Wpf/ChromiumWebBrowser.cs
index 45ec20cd7..503b319a1 100644
--- a/CefSharp.Wpf/ChromiumWebBrowser.cs
+++ b/CefSharp.Wpf/ChromiumWebBrowser.cs
@@ -1103,6 +1103,11 @@ namespace CefSharp.Wpf
/// The browser.
void IWebBrowserInternal.OnAfterBrowserCreated(IBrowser browser)
{
+ if (IsDisposed || browser.IsDisposed)
+ {
+ return;
+ }
+
Interlocked.Exchange(ref browserInitialized, 1);
this.browser = browser;