示例#1
0
        private void QueryResults()
        {
            if (!string.IsNullOrEmpty(QueryText))
            {
                var queryTimer = new System.Diagnostics.Stopwatch();
                queryTimer.Start();
                _updateSource?.Cancel();
                var currentUpdateSource = new CancellationTokenSource();
                _updateSource = currentUpdateSource;
                var currentCancellationToken = _updateSource.Token;
                _updateToken = currentCancellationToken;

                var query = QueryBuilder.Build(QueryText.Trim(), PluginManager.NonGlobalPlugins);
                if (query != null)
                {
                    _lastQuery = query;
                    Task.Run(() =>
                    {
                        Thread.Sleep(20);
                        RemoveOldQueryResults(query);
                        var plugins = PluginManager.ValidPluginsForQuery(query);

                        try
                        {
                            currentCancellationToken.ThrowIfCancellationRequested();
                            foreach (PluginPair plugin in plugins)
                            {
                                if (!plugin.Metadata.Disabled && !currentCancellationToken.IsCancellationRequested)
                                {
                                    var results = PluginManager.QueryForPlugin(plugin, query);
                                    currentCancellationToken.ThrowIfCancellationRequested();
                                    lock (_addResultsLock)
                                    {
                                        UpdateResultView(results, plugin.Metadata, query);
                                    }
                                }
                            }

                            currentCancellationToken.ThrowIfCancellationRequested();
                            Application.Current.Dispatcher.BeginInvoke(new Action(() =>
                            {
                                if (query.RawQuery == _lastQuery.RawQuery)
                                {
                                    Results.Results.NotifyChanges();
                                }

                                if (Results.Results.Count > 0)
                                {
                                    Results.Visibility    = Visibility.Visible;
                                    Results.SelectedIndex = 0;
                                }
                                else
                                {
                                    Results.Visibility = Visibility.Hidden;
                                }
                            }));
                        }
                        catch (OperationCanceledException)
                        {
                            // nothing to do here
                        }

                        queryTimer.Stop();
                        var queryEvent = new LauncherQueryEvent()
                        {
                            QueryTimeMs = queryTimer.ElapsedMilliseconds,
                            NumResults  = Results.Results.Count,
                            QueryLength = query.RawQuery.Length
                        };
                        PowerToysTelemetry.Log.WriteEvent(queryEvent);
                    }, currentCancellationToken);
                }
            }
            else
            {
                _updateSource?.Cancel();
                _lastQuery           = _emptyQuery;
                Results.SelectedItem = null;
                Results.Visibility   = Visibility.Hidden;
                Results.Clear();
            }
        }
示例#2
0
        private void QueryResults()
        {
            if (!string.IsNullOrEmpty(QueryText))
            {
                var queryTimer = new System.Diagnostics.Stopwatch();
                queryTimer.Start();
                _updateSource?.Cancel();
                var currentUpdateSource = new CancellationTokenSource();
                _updateSource = currentUpdateSource;
                var currentCancellationToken = _updateSource.Token;
                _updateToken = currentCancellationToken;

                var query = QueryBuilder.Build(QueryText.Trim(), PluginManager.NonGlobalPlugins);
                if (query != null)
                {
                    _currentQuery = query;
                    Task.Run(() =>
                    {
                        Thread.Sleep(20);
                        var plugins = PluginManager.ValidPluginsForQuery(query);

                        try
                        {
                            currentCancellationToken.ThrowIfCancellationRequested();

                            var resultPluginPair = new List <(List <Result>, PluginMetadata)>();
                            foreach (PluginPair plugin in plugins)
                            {
                                if (!plugin.Metadata.Disabled)
                                {
                                    var results = PluginManager.QueryForPlugin(plugin, query);
                                    resultPluginPair.Add((results, plugin.Metadata));
                                    currentCancellationToken.ThrowIfCancellationRequested();
                                }
                            }

                            lock (_addResultsLock)
                            {
                                if (query.RawQuery == _currentQuery.RawQuery)
                                {
                                    Results.Clear();
                                    foreach (var p in resultPluginPair)
                                    {
                                        UpdateResultView(p.Item1, query, currentCancellationToken);
                                        currentCancellationToken.ThrowIfCancellationRequested();
                                    }

                                    currentCancellationToken.ThrowIfCancellationRequested();
                                    Results.Sort();
                                }
                            }

                            currentCancellationToken.ThrowIfCancellationRequested();
                            UpdateResultsListViewAfterQuery(query);

                            // Run the slower query of the DelayedExecution plugins
                            currentCancellationToken.ThrowIfCancellationRequested();
                            Parallel.ForEach(plugins, (plugin) =>
                            {
                                if (!plugin.Metadata.Disabled)
                                {
                                    var results = PluginManager.QueryForPlugin(plugin, query, true);
                                    currentCancellationToken.ThrowIfCancellationRequested();
                                    if ((results?.Count ?? 0) != 0)
                                    {
                                        lock (_addResultsLock)
                                        {
                                            if (query.RawQuery == _currentQuery.RawQuery)
                                            {
                                                currentCancellationToken.ThrowIfCancellationRequested();

                                                // Remove the original results from the plugin
                                                Results.Results.RemoveAll(r => r.Result.PluginID == plugin.Metadata.ID);
                                                currentCancellationToken.ThrowIfCancellationRequested();

                                                // Add the new results from the plugin
                                                UpdateResultView(results, query, currentCancellationToken);
                                                currentCancellationToken.ThrowIfCancellationRequested();
                                                Results.Sort();
                                            }
                                        }

                                        currentCancellationToken.ThrowIfCancellationRequested();
                                        UpdateResultsListViewAfterQuery(query, true);
                                    }
                                }
                            });
                        }
                        catch (OperationCanceledException)
                        {
                            // nothing to do here
                        }

                        queryTimer.Stop();
                        var queryEvent = new LauncherQueryEvent()
                        {
                            QueryTimeMs = queryTimer.ElapsedMilliseconds,
                            NumResults  = Results.Results.Count,
                            QueryLength = query.RawQuery.Length
                        };
                        PowerToysTelemetry.Log.WriteEvent(queryEvent);
                    }, currentCancellationToken);
                }
            }
            else
            {
                _updateSource?.Cancel();
                _currentQuery        = _emptyQuery;
                Results.SelectedItem = null;
                Results.Visibility   = Visibility.Hidden;
                Results.Clear();
            }
        }
        private void QueryResults()
        {
            if (!string.IsNullOrEmpty(QueryText))
            {
                var queryTimer = new System.Diagnostics.Stopwatch();
                queryTimer.Start();
                _updateSource?.Cancel();
                var currentUpdateSource = new CancellationTokenSource();
                _updateSource = currentUpdateSource;
                var currentCancellationToken = _updateSource.Token;
                _updateToken = currentCancellationToken;

                ProgressBarVisibility = Visibility.Hidden;
                _isQueryRunning       = true;
                var query = QueryBuilder.Build(QueryText.Trim(), PluginManager.NonGlobalPlugins);
                if (query != null)
                {
                    // handle the exclusiveness of plugin using action keyword
                    RemoveOldQueryResults(query);

                    _lastQuery = query;
                    var plugins = PluginManager.ValidPluginsForQuery(query);

                    Task.Run(() =>
                    {
                        // so looping will stop once it was cancelled
                        var parallelOptions = new ParallelOptions {
                            CancellationToken = currentCancellationToken
                        };
                        try
                        {
                            Parallel.ForEach(plugins, parallelOptions, plugin =>
                            {
                                if (!plugin.Metadata.Disabled)
                                {
                                    var results = PluginManager.QueryForPlugin(plugin, query);
                                    if (Application.Current.Dispatcher.CheckAccess())
                                    {
                                        UpdateResultView(results, plugin.Metadata, query);
                                    }
                                    else
                                    {
                                        Application.Current.Dispatcher.BeginInvoke(new Action(() =>
                                        {
                                            UpdateResultView(results, plugin.Metadata, query);
                                        }));
                                    }
                                }
                            });
                        }
                        catch (OperationCanceledException)
                        {
                            // nothing to do here
                        }


                        // this should happen once after all queries are done so progress bar should continue
                        // until the end of all querying
                        _isQueryRunning = false;
                        if (currentUpdateSource == _updateSource)
                        { // update to hidden if this is still the current query
                            ProgressBarVisibility = Visibility.Hidden;
                        }

                        queryTimer.Stop();
                        var queryEvent = new LauncherQueryEvent()
                        {
                            QueryTimeMs = queryTimer.ElapsedMilliseconds,
                            NumResults  = Results.Results.Count,
                            QueryLength = query.RawQuery.Length
                        };
                        PowerToysTelemetry.Log.WriteEvent(queryEvent);
                    }, currentCancellationToken);
                }
            }
            else
            {
                Results.SelectedItem = null;
                Results.Clear();
                Results.Visibility = Visibility.Collapsed;
            }
        }
示例#4
0
        private void QueryResults()
        {
            if (!string.IsNullOrEmpty(QueryText))
            {
                var queryTimer = new System.Diagnostics.Stopwatch();
                queryTimer.Start();
                _updateSource?.Cancel();
                var currentUpdateSource = new CancellationTokenSource();
                _updateSource = currentUpdateSource;
                var currentCancellationToken = _updateSource.Token;
                _updateToken = currentCancellationToken;
                var queryText = QueryText.Trim();

                var pluginQueryPairs = QueryBuilder.Build(ref queryText, PluginManager.NonGlobalPlugins);
                if (pluginQueryPairs != null && pluginQueryPairs.Count > 0)
                {
                    _currentQuery = queryText;
                    Task.Run(
                        () =>
                    {
                        Thread.Sleep(20);

                        // Keep track of total number of results for telemetry
                        var numResults = 0;

                        // Contains all the plugins for which this raw query is valid
                        var plugins = pluginQueryPairs.Keys.ToList();

                        try
                        {
                            currentCancellationToken.ThrowIfCancellationRequested();

                            var resultPluginPair = new List <(List <Result>, PluginMetadata)>();

                            // To execute a query corresponding to each plugin
                            foreach (KeyValuePair <PluginPair, Query> pluginQueryItem in pluginQueryPairs)
                            {
                                var plugin = pluginQueryItem.Key;
                                var query  = pluginQueryItem.Value;

                                if (!plugin.Metadata.Disabled)
                                {
                                    var results = PluginManager.QueryForPlugin(plugin, query);
                                    resultPluginPair.Add((results, plugin.Metadata));
                                    currentCancellationToken.ThrowIfCancellationRequested();
                                }
                            }

                            lock (_addResultsLock)
                            {
                                // Using CurrentCultureIgnoreCase since this is user facing
                                if (queryText.Equals(_currentQuery, StringComparison.CurrentCultureIgnoreCase))
                                {
                                    Results.Clear();
                                    foreach (var p in resultPluginPair)
                                    {
                                        UpdateResultView(p.Item1, queryText, currentCancellationToken);
                                        currentCancellationToken.ThrowIfCancellationRequested();
                                    }

                                    currentCancellationToken.ThrowIfCancellationRequested();
                                    numResults = Results.Results.Count;
                                    Results.Sort();
                                    Results.SelectedItem = Results.Results.FirstOrDefault();
                                }
                            }

                            currentCancellationToken.ThrowIfCancellationRequested();

                            UpdateResultsListViewAfterQuery(queryText);

                            // Run the slower query of the DelayedExecution plugins
                            currentCancellationToken.ThrowIfCancellationRequested();
                            Parallel.ForEach(plugins, (plugin) =>
                            {
                                try
                                {
                                    if (!plugin.Metadata.Disabled)
                                    {
                                        Query query;
                                        pluginQueryPairs.TryGetValue(plugin, out query);

                                        var results = PluginManager.QueryForPlugin(plugin, query, true);
                                        currentCancellationToken.ThrowIfCancellationRequested();
                                        if ((results?.Count ?? 0) != 0)
                                        {
                                            lock (_addResultsLock)
                                            {
                                                // Using CurrentCultureIgnoreCase since this is user facing
                                                if (queryText.Equals(_currentQuery, StringComparison.CurrentCultureIgnoreCase))
                                                {
                                                    currentCancellationToken.ThrowIfCancellationRequested();

                                                    // Remove the original results from the plugin
                                                    Results.Results.RemoveAll(r => r.Result.PluginID == plugin.Metadata.ID);
                                                    currentCancellationToken.ThrowIfCancellationRequested();

                                                    // Add the new results from the plugin
                                                    UpdateResultView(results, queryText, currentCancellationToken);

                                                    currentCancellationToken.ThrowIfCancellationRequested();
                                                    numResults = Results.Results.Count;
                                                    Results.Sort();
                                                    Results.SelectedItem = Results.Results.FirstOrDefault();
                                                }
                                            }

                                            currentCancellationToken.ThrowIfCancellationRequested();
                                            UpdateResultsListViewAfterQuery(queryText, true);
                                        }
                                    }
                                }
                                catch (OperationCanceledException)
                                {
                                    // nothing to do here
                                }
                            });
                        }
                        catch (OperationCanceledException)
                        {
                            // nothing to do here
                        }

                        queryTimer.Stop();
                        var queryEvent = new LauncherQueryEvent()
                        {
                            QueryTimeMs = queryTimer.ElapsedMilliseconds,
                            NumResults  = numResults,
                            QueryLength = queryText.Length,
                        };
                        PowerToysTelemetry.Log.WriteEvent(queryEvent);
                    }, currentCancellationToken);
                }
            }
            else
            {
                _updateSource?.Cancel();
                _currentQuery        = _emptyQuery;
                Results.SelectedItem = null;
                Results.Visibility   = Visibility.Hidden;
                Task.Run(() =>
                {
                    lock (_addResultsLock)
                    {
                        Results.Clear();
                    }
                });
            }
        }