Skip to content

Commit

Permalink
fix #412
Browse files Browse the repository at this point in the history
- use Task instead of QueueUserWorkItem
- add CancellationTokenSource when updating result panel and executing
query for all plugins
  • Loading branch information
bao-qian committed Apr 25, 2016
1 parent 6edddb4 commit be4dbec
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 19 deletions.
14 changes: 5 additions & 9 deletions Wox/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,12 @@
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;

Expand Down Expand Up @@ -47,17 +44,16 @@ protected override void OnStartup(StartupEventArgs e)
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());
});
Expand Down
25 changes: 15 additions & 10 deletions Wox/ViewModel/MainViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ public class MainViewModel : BaseViewModel
private readonly UserSelectedRecord _userSelectedRecord;
private readonly TopMostRecord _topMostRecord;

private CancellationTokenSource _updateSource;
private CancellationToken _updateToken;

#endregion

#region Constructor
Expand Down Expand Up @@ -373,6 +376,10 @@ private void InitializeContextMenu()
private void HandleQueryTextUpdated()
{
IsProgressBarTooltipVisible = false;
_updateSource?.Cancel();
_updateSource = new CancellationTokenSource();
_updateToken = _updateSource.Token;

if (ContextMenuVisibility.IsVisible())
{
QueryContextMenu();
Expand Down Expand Up @@ -442,33 +449,31 @@ private void Query(string text)
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)
{
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);
}
}


}
}

Expand Down

0 comments on commit be4dbec

Please sign in to comment.