Fix environment exit stuck issue

This commit is contained in:
Jack251970 2025-04-03 23:01:16 +08:00
parent 2f53a79a26
commit b725975b98
2 changed files with 17 additions and 4 deletions

View file

@ -304,6 +304,14 @@ namespace Flow.Launcher
return;
}
// If we call Environment.Exit(0), the application dispose will be called before _mainWindow.Close()
// Accessing _mainWindow?.Dispatcher will cause the application stuck
// So here we need to check it and just return so that we will not acees _mainWindow?.Dispatcher
if (!_mainWindow.CanClose)
{
return;
}
_disposed = true;
}

View file

@ -32,6 +32,13 @@ namespace Flow.Launcher
{
public partial class MainWindow : IDisposable
{
#region Public Property
// Window Event: Close Event
public bool CanClose { get; set; } = false;
#endregion
#region Private Fields
// Dependency Injection
@ -45,8 +52,6 @@ namespace Flow.Launcher
private readonly ContextMenu _contextMenu = new();
private readonly MainViewModel _viewModel;
// Window Event: Close Event
private bool _canClose = false;
// Window Event: Key Event
private bool _isArrowKeyPressed = false;
@ -279,7 +284,7 @@ namespace Flow.Launcher
private async void OnClosing(object sender, CancelEventArgs e)
{
if (!_canClose)
if (!CanClose)
{
_notifyIcon.Visible = false;
App.API.SaveAppAllSettings();
@ -287,7 +292,7 @@ namespace Flow.Launcher
await PluginManager.DisposePluginsAsync();
Notification.Uninstall();
// After plugins are all disposed, we can close the main window
_canClose = true;
CanClose = true;
// Use this instead of Close() to avoid InvalidOperationException when calling Close() in OnClosing event
Application.Current.Shutdown();
}