- 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.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());
});

View File

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