diff --git a/Plugins/Flow.Launcher.Plugin.Explorer/Main.cs b/Plugins/Flow.Launcher.Plugin.Explorer/Main.cs index 30a06e882f3..9c4c1db8bb7 100644 --- a/Plugins/Flow.Launcher.Plugin.Explorer/Main.cs +++ b/Plugins/Flow.Launcher.Plugin.Explorer/Main.cs @@ -3,6 +3,7 @@ using Flow.Launcher.Plugin.Explorer.ViewModels; using Flow.Launcher.Plugin.Explorer.Views; using System.Collections.Generic; +using System.Threading; using System.Windows.Controls; namespace Flow.Launcher.Plugin.Explorer @@ -17,6 +18,9 @@ public class Main : ISettingProvider, IPlugin, ISavable, IContextMenu, IPluginI1 private IContextMenu contextMenu; + private static CancellationTokenSource updateSource; + public static CancellationToken updateToken; + public Control CreateSettingPanel() { return new ExplorerSettings(viewModel); @@ -37,6 +41,9 @@ public List LoadContextMenus(Result selectedResult) public List Query(Query query) { + updateSource?.Cancel(); + updateSource = new CancellationTokenSource(); + updateToken = updateSource.Token; return new SearchManager(Settings, Context).Search(query); } diff --git a/Plugins/Flow.Launcher.Plugin.Explorer/Search/WindowsIndex/IndexSearch.cs b/Plugins/Flow.Launcher.Plugin.Explorer/Search/WindowsIndex/IndexSearch.cs index 4f9325c7754..7e4e07b1053 100644 --- a/Plugins/Flow.Launcher.Plugin.Explorer/Search/WindowsIndex/IndexSearch.cs +++ b/Plugins/Flow.Launcher.Plugin.Explorer/Search/WindowsIndex/IndexSearch.cs @@ -2,9 +2,12 @@ using Microsoft.Search.Interop; using System; using System.Collections.Generic; +using System.Data; +using System.Data.Common; using System.Data.OleDb; using System.Linq; using System.Text.RegularExpressions; +using System.Threading.Tasks; namespace Flow.Launcher.Plugin.Explorer.Search.WindowsIndex { @@ -15,7 +18,7 @@ internal class IndexSearch private OleDbConnection conn; private OleDbCommand command; - + private OleDbDataReader dataReaderResults; private readonly ResultManager resultManager; @@ -28,7 +31,7 @@ internal IndexSearch(PluginInitContext context) resultManager = new ResultManager(context); } - internal List ExecuteWindowsIndexSearch(string indexQueryString, string connectionString, Query query) + internal async Task> ExecuteWindowsIndexSearch(string indexQueryString, string connectionString, Query query) { var folderResults = new List(); var fileResults = new List(); @@ -43,8 +46,11 @@ internal List ExecuteWindowsIndexSearch(string indexQueryString, string using (command = new OleDbCommand(indexQueryString, conn)) { // Results return as an OleDbDataReader. - using (dataReaderResults = command.ExecuteReader()) + var updateToken = Main.updateToken; + using (dataReaderResults = await command.ExecuteReaderAsync(updateToken).ConfigureAwait(false) as OleDbDataReader) { + if (updateToken.IsCancellationRequested) + return new List(); if (dataReaderResults.HasRows) { while (dataReaderResults.Read()) @@ -55,7 +61,7 @@ internal List ExecuteWindowsIndexSearch(string indexQueryString, string var encodedFragmentPath = dataReaderResults .GetString(1) .Replace("#", "%23", StringComparison.OrdinalIgnoreCase); - + var path = new Uri(encodedFragmentPath).LocalPath; if (dataReaderResults.GetString(2) == "Directory") @@ -63,7 +69,7 @@ internal List ExecuteWindowsIndexSearch(string indexQueryString, string folderResults.Add(resultManager.CreateFolderResult( dataReaderResults.GetString(0), path, - path, + path, query, true, true)); } else @@ -87,7 +93,7 @@ internal List ExecuteWindowsIndexSearch(string indexQueryString, string LogException("General error from performing index search", e); } - // Intial ordering, this order can be updated later by UpdateResultView.MainViewModel based on history of user selection. + // Initial ordering, this order can be updated later by UpdateResultView.MainViewModel based on history of user selection. return results.Concat(folderResults.OrderBy(x => x.Title)).Concat(fileResults.OrderBy(x => x.Title)).ToList(); ; } @@ -101,7 +107,7 @@ internal List WindowsIndexSearch(string searchString, string connectionS lock (_lock) { var constructedQuery = constructQuery(searchString); - return ExecuteWindowsIndexSearch(constructedQuery, connectionString, query); + return ExecuteWindowsIndexSearch(constructedQuery, connectionString, query).GetAwaiter().GetResult(); } }