Merge pull request #561 from taooceros/CloseWithDispose

Dispose Disposable or AsyncDisposable plugin on mainwindow close
This commit is contained in:
Jeremy Wu 2021-07-05 19:19:15 +10:00 committed by GitHub
commit 55e985faae
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 1 deletions

View file

@ -58,6 +58,22 @@ namespace Flow.Launcher.Core.Plugin
API.SavePluginSettings(); API.SavePluginSettings();
} }
public static async ValueTask DisposePluginsAsync()
{
foreach (var pluginPair in AllPlugins)
{
switch (pluginPair.Plugin)
{
case IDisposable disposable:
disposable.Dispose();
break;
case IAsyncDisposable asyncDisposable:
await asyncDisposable.DisposeAsync();
break;
}
}
}
public static async Task ReloadData() public static async Task ReloadData()
{ {
await Task.WhenAll(AllPlugins.Select(plugin => plugin.Plugin switch await Task.WhenAll(AllPlugins.Select(plugin => plugin.Plugin switch

View file

@ -11,6 +11,7 @@ using Flow.Launcher.Core.Resource;
using Flow.Launcher.Helper; using Flow.Launcher.Helper;
using Flow.Launcher.Infrastructure.UserSettings; using Flow.Launcher.Infrastructure.UserSettings;
using Flow.Launcher.ViewModel; using Flow.Launcher.ViewModel;
using Application = System.Windows.Application;
using Screen = System.Windows.Forms.Screen; using Screen = System.Windows.Forms.Screen;
using ContextMenuStrip = System.Windows.Forms.ContextMenuStrip; using ContextMenuStrip = System.Windows.Forms.ContextMenuStrip;
using DataFormats = System.Windows.DataFormats; using DataFormats = System.Windows.DataFormats;
@ -46,10 +47,13 @@ namespace Flow.Launcher
InitializeComponent(); InitializeComponent();
} }
private void OnClosing(object sender, CancelEventArgs e) private async void OnClosing(object sender, CancelEventArgs e)
{ {
_notifyIcon.Visible = false; _notifyIcon.Visible = false;
_viewModel.Save(); _viewModel.Save();
e.Cancel = true;
await PluginManager.DisposePluginsAsync();
Application.Current.Shutdown();
} }
private void OnInitialized(object sender, EventArgs e) private void OnInitialized(object sender, EventArgs e)