From b138838805e9542d7d362d6601be34993bbcce95 Mon Sep 17 00:00:00 2001 From: Jeremy Wu Date: Wed, 17 Mar 2021 06:32:53 +1100 Subject: [PATCH 1/3] add ConfigureWait and cancellation token --- .../Search/WindowsIndex/IndexSearch.cs | 46 ++++++++++++------- 1 file changed, 29 insertions(+), 17 deletions(-) diff --git a/Plugins/Flow.Launcher.Plugin.Explorer/Search/WindowsIndex/IndexSearch.cs b/Plugins/Flow.Launcher.Plugin.Explorer/Search/WindowsIndex/IndexSearch.cs index ecadd61ecb9..559ef4647f0 100644 --- a/Plugins/Flow.Launcher.Plugin.Explorer/Search/WindowsIndex/IndexSearch.cs +++ b/Plugins/Flow.Launcher.Plugin.Explorer/Search/WindowsIndex/IndexSearch.cs @@ -96,11 +96,12 @@ internal async static Task> WindowsIndexSearchAsync(string searchSt var constructedQuery = constructQuery(searchString); return await RemoveResultsInExclusionList( - await ExecuteWindowsIndexSearchAsync(constructedQuery, connectionString, query, token), - exclusionList); + await ExecuteWindowsIndexSearchAsync(constructedQuery, connectionString, query, token).ConfigureAwait(false), + exclusionList, + token).ConfigureAwait(false); } - private async static Task> RemoveResultsInExclusionList(List results, List exclusionList) + private async static Task> RemoveResultsInExclusionList(List results, List exclusionList, CancellationToken token) { var indexExclusionListCount = exclusionList.Count; @@ -109,27 +110,38 @@ private async static Task> RemoveResultsInExclusionList(List(); - await Task.Run(() => + try { - for (var index = 0; index < results.Count; index++) + await Task.Run(() => { - var excludeResult = false; - - for (var i = 0; i < indexExclusionListCount; i++) + for (var index = 0; index < results.Count; index++) { - if (results[index].SubTitle.StartsWith(exclusionList[i].Path, StringComparison.OrdinalIgnoreCase)) + token.ThrowIfCancellationRequested(); + + var excludeResult = false; + + for (var i = 0; i < indexExclusionListCount; i++) { - excludeResult = true; - break; + token.ThrowIfCancellationRequested(); + + if (results[index].SubTitle.StartsWith(exclusionList[i].Path, StringComparison.OrdinalIgnoreCase)) + { + excludeResult = true; + break; + } } - } - if (!excludeResult) - filteredResults.Add(results[index]); - } - }); + if (!excludeResult) + filteredResults.Add(results[index]); + } + }, token); - return filteredResults; + return filteredResults; + } + catch (OperationCanceledException) + { + return new List(); + } } internal static bool PathIsIndexed(string path) From f4971e2e96ad0b59ced9dd79fd9718e0c1ec0b5b Mon Sep 17 00:00:00 2001 From: Jeremy Wu Date: Thu, 18 Mar 2021 06:44:00 +1100 Subject: [PATCH 2/3] remove async and task run --- .../Search/WindowsIndex/IndexSearch.cs | 46 ++++++++----------- 1 file changed, 18 insertions(+), 28 deletions(-) diff --git a/Plugins/Flow.Launcher.Plugin.Explorer/Search/WindowsIndex/IndexSearch.cs b/Plugins/Flow.Launcher.Plugin.Explorer/Search/WindowsIndex/IndexSearch.cs index 559ef4647f0..174bdcfae17 100644 --- a/Plugins/Flow.Launcher.Plugin.Explorer/Search/WindowsIndex/IndexSearch.cs +++ b/Plugins/Flow.Launcher.Plugin.Explorer/Search/WindowsIndex/IndexSearch.cs @@ -95,13 +95,13 @@ internal async static Task> WindowsIndexSearchAsync(string searchSt return new List(); var constructedQuery = constructQuery(searchString); - return await RemoveResultsInExclusionList( + return RemoveResultsInExclusionList( await ExecuteWindowsIndexSearchAsync(constructedQuery, connectionString, query, token).ConfigureAwait(false), exclusionList, - token).ConfigureAwait(false); + token); } - private async static Task> RemoveResultsInExclusionList(List results, List exclusionList, CancellationToken token) + private static List RemoveResultsInExclusionList(List results, List exclusionList, CancellationToken token) { var indexExclusionListCount = exclusionList.Count; @@ -110,38 +110,28 @@ private async static Task> RemoveResultsInExclusionList(List(); - try + for (var index = 0; index < results.Count; index++) { - await Task.Run(() => - { - for (var index = 0; index < results.Count; index++) - { - token.ThrowIfCancellationRequested(); - - var excludeResult = false; + token.ThrowIfCancellationRequested(); - for (var i = 0; i < indexExclusionListCount; i++) - { - token.ThrowIfCancellationRequested(); + var excludeResult = false; - if (results[index].SubTitle.StartsWith(exclusionList[i].Path, StringComparison.OrdinalIgnoreCase)) - { - excludeResult = true; - break; - } - } + for (var i = 0; i < indexExclusionListCount; i++) + { + token.ThrowIfCancellationRequested(); - if (!excludeResult) - filteredResults.Add(results[index]); + if (results[index].SubTitle.StartsWith(exclusionList[i].Path, StringComparison.OrdinalIgnoreCase)) + { + excludeResult = true; + break; } - }, token); + } - return filteredResults; - } - catch (OperationCanceledException) - { - return new List(); + if (!excludeResult) + filteredResults.Add(results[index]); } + + return filteredResults; } internal static bool PathIsIndexed(string path) From 947992353145a5ba3ce5f08549d7877b017b3580 Mon Sep 17 00:00:00 2001 From: Jeremy Wu Date: Thu, 18 Mar 2021 06:50:55 +1100 Subject: [PATCH 3/3] remove unnecessary catching OperationCanceledException --- .../Search/WindowsIndex/IndexSearch.cs | 4 -- Plugins/Flow.Launcher.Plugin.Program/Main.cs | 42 +++++++------------ .../SuggestionSources/Baidu.cs | 5 --- .../SuggestionSources/Bing.cs | 4 -- .../SuggestionSources/Google.cs | 4 -- 5 files changed, 14 insertions(+), 45 deletions(-) diff --git a/Plugins/Flow.Launcher.Plugin.Explorer/Search/WindowsIndex/IndexSearch.cs b/Plugins/Flow.Launcher.Plugin.Explorer/Search/WindowsIndex/IndexSearch.cs index 174bdcfae17..22d89ba8fff 100644 --- a/Plugins/Flow.Launcher.Plugin.Explorer/Search/WindowsIndex/IndexSearch.cs +++ b/Plugins/Flow.Launcher.Plugin.Explorer/Search/WindowsIndex/IndexSearch.cs @@ -63,10 +63,6 @@ internal async static Task> ExecuteWindowsIndexSearchAsync(string i } } } - catch (OperationCanceledException) - { - return new List(); // The source code indicates that without adding members, it won't allocate an array - } catch (InvalidOperationException e) { // Internal error from ExecuteReader(): Connection closed. diff --git a/Plugins/Flow.Launcher.Plugin.Program/Main.cs b/Plugins/Flow.Launcher.Plugin.Program/Main.cs index d0347fa670c..0f6968092b2 100644 --- a/Plugins/Flow.Launcher.Plugin.Program/Main.cs +++ b/Plugins/Flow.Launcher.Plugin.Program/Main.cs @@ -50,35 +50,21 @@ public async Task> QueryAsync(Query query, CancellationToken token) win32 = _win32s; uwps = _uwps; - try + var result = await Task.Run(delegate { - var result = await Task.Run(delegate - { - try - { - return win32.Cast() - .Concat(uwps) - .AsParallel() - .WithCancellation(token) - .Where(p => p.Enabled) - .Select(p => p.Result(query.Search, _context.API)) - .Where(r => r?.Score > 0) - .ToList(); - } - catch (OperationCanceledException) - { - return null; - } - }, token).ConfigureAwait(false); - - token.ThrowIfCancellationRequested(); - - return result; - } - catch (OperationCanceledException) - { - return null; - } + return win32.Cast() + .Concat(uwps) + .AsParallel() + .WithCancellation(token) + .Where(p => p.Enabled) + .Select(p => p.Result(query.Search, _context.API)) + .Where(r => r?.Score > 0) + .ToList(); + }, token).ConfigureAwait(false); + + token.ThrowIfCancellationRequested(); + + return result; } public async Task InitAsync(PluginInitContext context) diff --git a/Plugins/Flow.Launcher.Plugin.WebSearch/SuggestionSources/Baidu.cs b/Plugins/Flow.Launcher.Plugin.WebSearch/SuggestionSources/Baidu.cs index 94458ff5203..ccb5b20d730 100644 --- a/Plugins/Flow.Launcher.Plugin.WebSearch/SuggestionSources/Baidu.cs +++ b/Plugins/Flow.Launcher.Plugin.WebSearch/SuggestionSources/Baidu.cs @@ -30,11 +30,6 @@ public override async Task> Suggestions(string query, CancellationT Log.Exception("|Baidu.Suggestions|Can't get suggestion from baidu", e); return null; } - catch (OperationCanceledException) - { - return null; - } - if (string.IsNullOrEmpty(result)) return new List(); Match match = _reg.Match(result); diff --git a/Plugins/Flow.Launcher.Plugin.WebSearch/SuggestionSources/Bing.cs b/Plugins/Flow.Launcher.Plugin.WebSearch/SuggestionSources/Bing.cs index 966a6ca16db..38c5fb4a0f8 100644 --- a/Plugins/Flow.Launcher.Plugin.WebSearch/SuggestionSources/Bing.cs +++ b/Plugins/Flow.Launcher.Plugin.WebSearch/SuggestionSources/Bing.cs @@ -45,10 +45,6 @@ public override async Task> Suggestions(string query, CancellationT Log.Exception("|Baidu.Suggestions|Can't get suggestion from baidu", e); return null; } - catch (OperationCanceledException) - { - return new List(); - } catch (JsonException e) { Log.Exception("|Bing.Suggestions|can't parse suggestions", e); diff --git a/Plugins/Flow.Launcher.Plugin.WebSearch/SuggestionSources/Google.cs b/Plugins/Flow.Launcher.Plugin.WebSearch/SuggestionSources/Google.cs index 91ab921b634..c5f43d081be 100644 --- a/Plugins/Flow.Launcher.Plugin.WebSearch/SuggestionSources/Google.cs +++ b/Plugins/Flow.Launcher.Plugin.WebSearch/SuggestionSources/Google.cs @@ -37,10 +37,6 @@ public override async Task> Suggestions(string query, CancellationT Log.Exception("|Baidu.Suggestions|Can't get suggestion from baidu", e); return null; } - catch (OperationCanceledException) - { - return new List(); - } catch (JsonException e) { Log.Exception("|Google.Suggestions|can't parse suggestions", e);