From a18a777e75622e4c02393c3c3538c4778ba7d052 Mon Sep 17 00:00:00 2001 From: Kevin Zhang Date: Mon, 5 Jul 2021 16:50:50 +0800 Subject: [PATCH 1/2] Dispose Disposable or AsyncDisposable plugin on mainwindow close --- Flow.Launcher.Core/Plugin/PluginManager.cs | 16 ++++++++++++++++ Flow.Launcher/MainWindow.xaml.cs | 6 +++++- 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/Flow.Launcher.Core/Plugin/PluginManager.cs b/Flow.Launcher.Core/Plugin/PluginManager.cs index a2a00932954..41a47b3d65b 100644 --- a/Flow.Launcher.Core/Plugin/PluginManager.cs +++ b/Flow.Launcher.Core/Plugin/PluginManager.cs @@ -58,6 +58,22 @@ public static void Save() API.SavePluginSettings(); } + public static async ValueTask DisposePluginsAsync() + { + foreach (var plugin in AllPlugins) + { + switch (plugin) + { + case IDisposable disposable: + disposable.Dispose(); + break; + case IAsyncDisposable asyncDisposable: + await asyncDisposable.DisposeAsync(); + break; + } + } + } + public static async Task ReloadData() { await Task.WhenAll(AllPlugins.Select(plugin => plugin.Plugin switch diff --git a/Flow.Launcher/MainWindow.xaml.cs b/Flow.Launcher/MainWindow.xaml.cs index 735103938f0..5fab812e6e2 100644 --- a/Flow.Launcher/MainWindow.xaml.cs +++ b/Flow.Launcher/MainWindow.xaml.cs @@ -11,6 +11,7 @@ using Flow.Launcher.Helper; using Flow.Launcher.Infrastructure.UserSettings; using Flow.Launcher.ViewModel; +using Application = System.Windows.Application; using Screen = System.Windows.Forms.Screen; using ContextMenuStrip = System.Windows.Forms.ContextMenuStrip; using DataFormats = System.Windows.DataFormats; @@ -46,10 +47,13 @@ public MainWindow() InitializeComponent(); } - private void OnClosing(object sender, CancelEventArgs e) + private async void OnClosing(object sender, CancelEventArgs e) { _notifyIcon.Visible = false; _viewModel.Save(); + e.Cancel = true; + await PluginManager.DisposePluginsAsync(); + Application.Current.Shutdown(); } private void OnInitialized(object sender, EventArgs e) From e2811ce747fd50db6394cb31c33d38e147c17452 Mon Sep 17 00:00:00 2001 From: Kevin Zhang Date: Mon, 5 Jul 2021 16:54:19 +0800 Subject: [PATCH 2/2] dispose PluginPair.Plugin instead of PluginPair --- Flow.Launcher.Core/Plugin/PluginManager.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Flow.Launcher.Core/Plugin/PluginManager.cs b/Flow.Launcher.Core/Plugin/PluginManager.cs index 41a47b3d65b..134c3c002dc 100644 --- a/Flow.Launcher.Core/Plugin/PluginManager.cs +++ b/Flow.Launcher.Core/Plugin/PluginManager.cs @@ -60,9 +60,9 @@ public static void Save() public static async ValueTask DisposePluginsAsync() { - foreach (var plugin in AllPlugins) + foreach (var pluginPair in AllPlugins) { - switch (plugin) + switch (pluginPair.Plugin) { case IDisposable disposable: disposable.Dispose();