From 3ae656f456be00a5349738869aa8a1f64568a8c8 Mon Sep 17 00:00:00 2001 From: Vic <10308169+VictoriousRaptor@users.noreply.github.com> Date: Mon, 24 Apr 2023 00:45:38 +0800 Subject: [PATCH] Requery after killing a process --- .../Main.cs | 38 ++++++++++--------- .../ProcessHelper.cs | 2 +- 2 files changed, 22 insertions(+), 18 deletions(-) diff --git a/Plugins/Flow.Launcher.Plugin.ProcessKiller/Main.cs b/Plugins/Flow.Launcher.Plugin.ProcessKiller/Main.cs index 19f96aea1..3eff7e398 100644 --- a/Plugins/Flow.Launcher.Plugin.ProcessKiller/Main.cs +++ b/Plugins/Flow.Launcher.Plugin.ProcessKiller/Main.cs @@ -1,12 +1,7 @@ -using System; -using System.Collections.Generic; +using System.Collections.Generic; using System.Linq; -using System.Text; -using System.Diagnostics; -using System.Dynamic; -using System.Runtime.InteropServices; using Flow.Launcher.Infrastructure; -using Flow.Launcher.Infrastructure.Logger; +using System.Threading.Tasks; namespace Flow.Launcher.Plugin.ProcessKiller { @@ -23,13 +18,7 @@ namespace Flow.Launcher.Plugin.ProcessKiller public List Query(Query query) { - var termToSearch = query.Search; - - var processlist = processHelper.GetMatchingProcesses(termToSearch); - - return !processlist.Any() - ? null - : CreateResultsFromProcesses(processlist, termToSearch); + return CreateResultsFromQuery(query); } public string GetTranslatedPluginTitle() @@ -50,7 +39,7 @@ namespace Flow.Launcher.Plugin.ProcessKiller // get all non-system processes whose file path matches that of the given result (processPath) var similarProcesses = processHelper.GetSimilarProcesses(processPath); - if (similarProcesses.Count() > 0) + if (similarProcesses.Any()) { menuOptions.Add(new Result { @@ -72,8 +61,16 @@ namespace Flow.Launcher.Plugin.ProcessKiller return menuOptions; } - private List CreateResultsFromProcesses(List processlist, string termToSearch) + private List CreateResultsFromQuery(Query query) { + string termToSearch = query.Search; + var processlist = processHelper.GetMatchingProcesses(termToSearch); + + if (!processlist.Any()) + { + return null; + } + var results = new List(); foreach (var pr in processlist) @@ -92,6 +89,7 @@ namespace Flow.Launcher.Plugin.ProcessKiller Action = (c) => { processHelper.TryKill(p); + _ = DelayAndReQueryAsync(query.RawQuery); // Re-query after killing process to refresh process list return true; } }); @@ -116,7 +114,7 @@ namespace Flow.Launcher.Plugin.ProcessKiller { processHelper.TryKill(p.Process); } - + _ = DelayAndReQueryAsync(query.RawQuery); // Re-query after killing process to refresh process list return true; } }); @@ -124,5 +122,11 @@ namespace Flow.Launcher.Plugin.ProcessKiller return sortedResults; } + + private static async Task DelayAndReQueryAsync(string query) + { + await Task.Delay(500); + _context.API.ChangeQuery(query, true); + } } } diff --git a/Plugins/Flow.Launcher.Plugin.ProcessKiller/ProcessHelper.cs b/Plugins/Flow.Launcher.Plugin.ProcessKiller/ProcessHelper.cs index 16a8687e6..0932955d6 100644 --- a/Plugins/Flow.Launcher.Plugin.ProcessKiller/ProcessHelper.cs +++ b/Plugins/Flow.Launcher.Plugin.ProcessKiller/ProcessHelper.cs @@ -79,7 +79,7 @@ namespace Flow.Launcher.Plugin.ProcessKiller } catch (Exception e) { - Log.Exception($"|ProcessKiller.CreateResultsFromProcesses|Failed to kill process {p.ProcessName}", e); + Log.Exception($"{nameof(ProcessHelper)}", $"Failed to kill process {p.ProcessName}", e); } }