Skip to content

Commit 75fd3ac

Browse files
committed
WinForms - LifeSpanHandler.DoClose add ObjectDisposedException try/catch
- If the popup is being hosted on a Form that is being Closed/Disposed as we attempt to call Control.Invoke we can end up with an ObjectDisposedException
1 parent 0c7bc3f commit 75fd3ac

File tree

1 file changed

+18
-7
lines changed

1 file changed

+18
-7
lines changed

CefSharp.WinForms/Handler/LifeSpanHandler.cs

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -105,15 +105,26 @@ protected override bool DoClose(IWebBrowser chromiumWebBrowser, IBrowser browser
105105
//need to remove the popup (likely removed from menu)
106106
if (!control.IsDisposed && control.IsHandleCreated)
107107
{
108-
//We need to invoke in a sync fashion so our IBrowser object is still in scope
109-
//Calling in an async fashion leads to the IBrowser being disposed before we
110-
//can access it.
111-
control.InvokeSyncOnUiThreadIfRequired(new Action(() =>
108+
try
112109
{
113-
onPopupDestroyed?.Invoke(control, browser);
110+
//We need to invoke in a sync fashion so our IBrowser object is still in scope
111+
//Calling in an async fashion leads to the IBrowser being disposed before we
112+
//can access it.
113+
control.InvokeSyncOnUiThreadIfRequired(new Action(() =>
114+
{
115+
onPopupDestroyed?.Invoke(control, browser);
114116

115-
control.Dispose();
116-
}));
117+
control.Dispose();
118+
}));
119+
}
120+
catch (ObjectDisposedException)
121+
{
122+
// If the popup is being hosted on a Form that is being
123+
// Closed/Disposed as we attempt to call Control.Invoke
124+
// we can end up with an ObjectDisposedException
125+
// return false (Default behaviour).
126+
return false;
127+
}
117128
}
118129
}
119130

0 commit comments

Comments
 (0)