Requery after killing a process

This commit is contained in:
Vic 2023-04-24 00:45:38 +08:00 committed by VictoriousRaptor
parent 2147943117
commit 3ae656f456
2 changed files with 22 additions and 18 deletions

View file

@ -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<Result> 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<Result> CreateResultsFromProcesses(List<ProcessResult> processlist, string termToSearch)
private List<Result> CreateResultsFromQuery(Query query)
{
string termToSearch = query.Search;
var processlist = processHelper.GetMatchingProcesses(termToSearch);
if (!processlist.Any())
{
return null;
}
var results = new List<Result>();
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);
}
}
}

View file

@ -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);
}
}