From 99006465e60818729d11a021ae30570f3664864f Mon Sep 17 00:00:00 2001 From: bao-qian Date: Mon, 25 Apr 2016 02:33:55 +0100 Subject: [PATCH] fix #412 - use Task instead of QueueUserWorkItem - add CancellationTokenSource when updating result panel and executing query for all plugins --- Wox/App.xaml.cs | 14 +++++--------- Wox/ViewModel/MainViewModel.cs | 25 +++++++++++++++---------- 2 files changed, 20 insertions(+), 19 deletions(-) diff --git a/Wox/App.xaml.cs b/Wox/App.xaml.cs index 95d6a71480..ac43e4bb4b 100644 --- a/Wox/App.xaml.cs +++ b/Wox/App.xaml.cs @@ -5,15 +5,12 @@ using System.IO; using System.Linq; using System.Reflection; using System.Threading; +using System.Threading.Tasks; using System.Windows; using Wox.CommandArgs; using Wox.Core.Plugin; -using Wox.Core.Resource; -using Wox.Core.Updater; -using Wox.Core.UserSettings; using Wox.Helper; using Wox.Infrastructure; -using Wox.Infrastructure.Storage; using Wox.ViewModel; using Stopwatch = Wox.Infrastructure.Stopwatch; @@ -47,17 +44,16 @@ namespace Wox RegisterUnhandledException(); ImageLoader = new ImageLoader.ImageLoader(); - ThreadPool.QueueUserWorkItem(_ => { ImageLoader.PreloadImages(); }); - + Task.Factory.StartNew(ImageLoader.PreloadImages); PluginManager.Initialize(); - - MainViewModel mainVM = new MainViewModel(); + + MainViewModel mainVM = new MainViewModel(); API = new PublicAPIInstance(mainVM, mainVM._settings); PluginManager.InitializePlugins(API); mainVM._settings.UpdatePluginSettings(); - Window = new MainWindow (mainVM._settings, mainVM); + Window = new MainWindow(mainVM._settings, mainVM); NotifyIconManager notifyIconManager = new NotifyIconManager(API); CommandArgsFactory.Execute(e.Args.ToList()); }); diff --git a/Wox/ViewModel/MainViewModel.cs b/Wox/ViewModel/MainViewModel.cs index d6ff813ea8..c298d8c760 100644 --- a/Wox/ViewModel/MainViewModel.cs +++ b/Wox/ViewModel/MainViewModel.cs @@ -48,6 +48,9 @@ namespace Wox.ViewModel private readonly UserSelectedRecord _userSelectedRecord; private readonly TopMostRecord _topMostRecord; + private CancellationTokenSource _updateSource; + private CancellationToken _updateToken; + #endregion #region Constructor @@ -373,6 +376,10 @@ namespace Wox.ViewModel private void HandleQueryTextUpdated() { IsProgressBarTooltipVisible = false; + _updateSource?.Cancel(); + _updateSource = new CancellationTokenSource(); + _updateToken = _updateSource.Token; + if (ContextMenuVisibility.IsVisible()) { QueryContextMenu(); @@ -442,17 +449,15 @@ namespace Wox.ViewModel Results.RemoveResultsExcept(PluginManager.NonGlobalPlugins[keyword].Metadata); } } - _lastQuery = query; - Action action = async () => + _lastQuery = query; + Task.Delay(200, _updateToken).ContinueWith(_ => { - await Task.Delay(150); - if (!string.IsNullOrEmpty(query.RawQuery) && query.RawQuery == _lastQuery.RawQuery && !_queryHasReturn) + if (query.RawQuery == _lastQuery.RawQuery && !_queryHasReturn) { - IsProgressBarTooltipVisible = true; + ProgressBarVisibility = Visibility.Visible; } - }; - action.Invoke(); + }, _updateToken); var plugins = PluginManager.ValidPluginsForQuery(query); foreach (var plugin in plugins) @@ -460,15 +465,15 @@ namespace Wox.ViewModel var config = _settings.PluginSettings[plugin.Metadata.ID]; if (!config.Disabled) { - ThreadPool.QueueUserWorkItem(o => + Task.Factory.StartNew(() => { var results = PluginManager.QueryForPlugin(plugin, query); UpdateResultView(results, plugin.Metadata, query); - }); + }, _updateToken); } } - + } }