Improve Shutdown - Use Dispatcher to Dispose of ChromiumWebBrowser instances where possible

- Hook the DispatcherShutdownStarted and DispatcherShutdownFinished events then call Cef.PreShutdown

https://github.com/cefsharp/CefSharp/commit/6b440a84a3652ed0d4f5a299b566e731dae9aa4c#diff-b27c014d78a0c17c1c8fe22fc001dcc3f8c02617541b121258af40ada3f928e4
This commit is contained in:
Alex Maitland
2021-09-03 12:55:53 +10:00
parent f8d07f6856
commit c5c6d486a1
+78 -27
View File
@@ -477,15 +477,89 @@ namespace CefSharp.Wpf.HwndHost
{
if (CefSharpSettings.ShutdownOnExit)
{
var app = Application.Current;
if (app != null)
//Use Dispatcher.FromThread as it returns null if no dispatcher
//is available for this thread.
var dispatcher = Dispatcher.FromThread(Thread.CurrentThread);
if (dispatcher == null)
{
app.Exit += OnApplicationExit;
//No dispatcher then we'll rely on Application.Exit
var app = Application.Current;
if (app != null)
{
app.Exit += OnApplicationExit;
}
}
else
{
dispatcher.ShutdownStarted += DispatcherShutdownStarted;
dispatcher.ShutdownFinished += DispatcherShutdownFinished;
}
}
}
/// <summary>
/// Handles Dispatcher Shutdown
/// </summary>
/// <param name="sender">sender</param>
/// <param name="e">eventargs</param>
private static void DispatcherShutdownStarted(object sender, EventArgs e)
{
var dispatcher = (Dispatcher)sender;
dispatcher.ShutdownStarted -= DispatcherShutdownStarted;
if (!DesignMode)
{
CefPreShutdown();
}
}
private static void DispatcherShutdownFinished(object sender, EventArgs e)
{
var dispatcher = (Dispatcher)sender;
dispatcher.ShutdownFinished -= DispatcherShutdownFinished;
if (!DesignMode)
{
CefShutdown();
}
}
/// <summary>
/// Handles the <see cref="E:ApplicationExit" /> event.
/// </summary>
/// <param name="sender">The sender.</param>
/// <param name="e">The <see cref="ExitEventArgs"/> instance containing the event data.</param>
private static void OnApplicationExit(object sender, ExitEventArgs e)
{
if (!DesignMode)
{
CefShutdown();
}
}
/// <summary>
/// Required for designer support - this method cannot be inlined as the designer
/// will attempt to load libcef.dll and will subsequently throw an exception.
/// </summary>
[MethodImpl(MethodImplOptions.NoInlining)]
private static void CefPreShutdown()
{
Cef.PreShutdown();
}
/// <summary>
/// Required for designer support - this method cannot be inlined as the designer
/// will attempt to load libcef.dll and will subsiquently throw an exception.
/// </summary>
[MethodImpl(MethodImplOptions.NoInlining)]
private static void CefShutdown()
{
Cef.Shutdown();
}
/// <summary>
/// Initializes a new instance of the <see cref="ChromiumWebBrowser"/> instance.
/// </summary>
@@ -1383,29 +1457,6 @@ namespace CefSharp.Wpf.HwndHost
}
}
/// <summary>
/// Handles the <see cref="E:ApplicationExit" /> event.
/// </summary>
/// <param name="sender">The sender.</param>
/// <param name="e">The <see cref="ExitEventArgs"/> instance containing the event data.</param>
private static void OnApplicationExit(object sender, ExitEventArgs e)
{
if (!DesignMode)
{
CefShutdown();
}
}
/// <summary>
/// Required for designer support - this method cannot be inlined as the designer
/// will attempt to load libcef.dll and will subsiquently throw an exception.
/// </summary>
[MethodImpl(MethodImplOptions.NoInlining)]
private static void CefShutdown()
{
Cef.Shutdown();
}
/// <summary>
/// Loads the specified URL.
/// </summary>