- use Task instead of QueueUserWorkItem
- add CancellationTokenSource when updating result panel and executing
query for all plugins
This commit is contained in:
bao-qian 2016-04-25 02:33:55 +01:00
parent 4a7e9c16d0
commit 99006465e6
2 changed files with 20 additions and 19 deletions

View File

@ -5,15 +5,12 @@ using System.IO;
using System.Linq; using System.Linq;
using System.Reflection; using System.Reflection;
using System.Threading; using System.Threading;
using System.Threading.Tasks;
using System.Windows; using System.Windows;
using Wox.CommandArgs; using Wox.CommandArgs;
using Wox.Core.Plugin; using Wox.Core.Plugin;
using Wox.Core.Resource;
using Wox.Core.Updater;
using Wox.Core.UserSettings;
using Wox.Helper; using Wox.Helper;
using Wox.Infrastructure; using Wox.Infrastructure;
using Wox.Infrastructure.Storage;
using Wox.ViewModel; using Wox.ViewModel;
using Stopwatch = Wox.Infrastructure.Stopwatch; using Stopwatch = Wox.Infrastructure.Stopwatch;
@ -47,8 +44,7 @@ namespace Wox
RegisterUnhandledException(); RegisterUnhandledException();
ImageLoader = new ImageLoader.ImageLoader(); ImageLoader = new ImageLoader.ImageLoader();
ThreadPool.QueueUserWorkItem(_ => { ImageLoader.PreloadImages(); }); Task.Factory.StartNew(ImageLoader.PreloadImages);
PluginManager.Initialize(); PluginManager.Initialize();
MainViewModel mainVM = new MainViewModel(); MainViewModel mainVM = new MainViewModel();

View File

@ -48,6 +48,9 @@ namespace Wox.ViewModel
private readonly UserSelectedRecord _userSelectedRecord; private readonly UserSelectedRecord _userSelectedRecord;
private readonly TopMostRecord _topMostRecord; private readonly TopMostRecord _topMostRecord;
private CancellationTokenSource _updateSource;
private CancellationToken _updateToken;
#endregion #endregion
#region Constructor #region Constructor
@ -373,6 +376,10 @@ namespace Wox.ViewModel
private void HandleQueryTextUpdated() private void HandleQueryTextUpdated()
{ {
IsProgressBarTooltipVisible = false; IsProgressBarTooltipVisible = false;
_updateSource?.Cancel();
_updateSource = new CancellationTokenSource();
_updateToken = _updateSource.Token;
if (ContextMenuVisibility.IsVisible()) if (ContextMenuVisibility.IsVisible())
{ {
QueryContextMenu(); QueryContextMenu();
@ -442,17 +449,15 @@ namespace Wox.ViewModel
Results.RemoveResultsExcept(PluginManager.NonGlobalPlugins[keyword].Metadata); Results.RemoveResultsExcept(PluginManager.NonGlobalPlugins[keyword].Metadata);
} }
} }
_lastQuery = query;
Action action = async () => _lastQuery = query;
Task.Delay(200, _updateToken).ContinueWith(_ =>
{ {
await Task.Delay(150); if (query.RawQuery == _lastQuery.RawQuery && !_queryHasReturn)
if (!string.IsNullOrEmpty(query.RawQuery) && query.RawQuery == _lastQuery.RawQuery && !_queryHasReturn)
{ {
IsProgressBarTooltipVisible = true; ProgressBarVisibility = Visibility.Visible;
} }
}; }, _updateToken);
action.Invoke();
var plugins = PluginManager.ValidPluginsForQuery(query); var plugins = PluginManager.ValidPluginsForQuery(query);
foreach (var plugin in plugins) foreach (var plugin in plugins)
@ -460,11 +465,11 @@ namespace Wox.ViewModel
var config = _settings.PluginSettings[plugin.Metadata.ID]; var config = _settings.PluginSettings[plugin.Metadata.ID];
if (!config.Disabled) if (!config.Disabled)
{ {
ThreadPool.QueueUserWorkItem(o => Task.Factory.StartNew(() =>
{ {
var results = PluginManager.QueryForPlugin(plugin, query); var results = PluginManager.QueryForPlugin(plugin, query);
UpdateResultView(results, plugin.Metadata, query); UpdateResultView(results, plugin.Metadata, query);
}); }, _updateToken);
} }
} }