mirror of
https://github.com/Tyrrrz/DiscordChatExporter.git
synced 2026-05-26 18:52:05 +00:00
Guard App.Dispose() against double-invocation and late shutdown (#1499)
Co-authored-by: Tyrrrz <1935960+Tyrrrz@users.noreply.github.com> Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
This commit is contained in:
@@ -26,6 +26,8 @@ public class App : Application, IDisposable
|
|||||||
private readonly SettingsService _settingsService;
|
private readonly SettingsService _settingsService;
|
||||||
private readonly MainViewModel _mainViewModel;
|
private readonly MainViewModel _mainViewModel;
|
||||||
|
|
||||||
|
private bool _isDisposed;
|
||||||
|
|
||||||
public App()
|
public App()
|
||||||
{
|
{
|
||||||
var services = new ServiceCollection();
|
var services = new ServiceCollection();
|
||||||
@@ -98,8 +100,24 @@ public class App : Application, IDisposable
|
|||||||
public override void OnFrameworkInitializationCompleted()
|
public override void OnFrameworkInitializationCompleted()
|
||||||
{
|
{
|
||||||
if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
|
if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
|
||||||
|
{
|
||||||
desktop.MainWindow = new MainView { DataContext = _mainViewModel };
|
desktop.MainWindow = new MainView { DataContext = _mainViewModel };
|
||||||
|
|
||||||
|
void OnExit(object? sender, ControlledApplicationLifetimeExitEventArgs args)
|
||||||
|
{
|
||||||
|
if (sender is IControlledApplicationLifetime lifetime)
|
||||||
|
lifetime.Exit -= OnExit;
|
||||||
|
|
||||||
|
Dispose();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Although `App.Dispose()` is invoked from `Program.Main(...)`, on some platforms
|
||||||
|
// it may be called too late in the shutdown lifecycle. Attach an exit
|
||||||
|
// handler to ensure timely disposal as a safeguard.
|
||||||
|
// https://github.com/Tyrrrz/YoutubeDownloader/issues/795
|
||||||
|
desktop.Exit += OnExit;
|
||||||
|
}
|
||||||
|
|
||||||
base.OnFrameworkInitializationCompleted();
|
base.OnFrameworkInitializationCompleted();
|
||||||
|
|
||||||
// Set up custom theme colors
|
// Set up custom theme colors
|
||||||
@@ -115,6 +133,11 @@ public class App : Application, IDisposable
|
|||||||
|
|
||||||
public void Dispose()
|
public void Dispose()
|
||||||
{
|
{
|
||||||
|
if (_isDisposed)
|
||||||
|
return;
|
||||||
|
|
||||||
|
_isDisposed = true;
|
||||||
|
|
||||||
_eventRoot.Dispose();
|
_eventRoot.Dispose();
|
||||||
_services.Dispose();
|
_services.Dispose();
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user