protected override void OnStartSearch() { _ = _threadingContext.JoinableTaskFactory.RunAsync( async() => { await _threadingContext.JoinableTaskFactory.SwitchToMainThreadAsync(); foreach (var control in _controls) { _ = control.SetFilter( string.Empty, new SearchFilter(SearchQuery, control) ); } await TaskScheduler.Default; uint resultCount = 0; foreach (var control in _controls) { var results = await control.ForceUpdateAsync().ConfigureAwait(false); resultCount += (uint)results.FilteredAndSortedEntries.Count; } await _threadingContext.JoinableTaskFactory.SwitchToMainThreadAsync(); SearchCallback.ReportComplete(this, dwResultsFound: resultCount); } ); }
protected virtual void OnStartSearch() { if (!SetTaskStatus(SearchTaskStatus.Completed)) { return; } SearchCallback.ReportComplete(this, SearchResults); }
protected override void OnStartSearch() { ThreadHelper.JoinableTaskFactory.RunAsync(async() => { await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync(); _control.SetFilter(TableToolWindow.SearchFilterKey, new TableSearchFilter(SearchQuery, _control)); SearchCallback.ReportComplete(this, dwResultsFound: 0); }); }
public void Stop() { lock (_syncObj) { if (!SetTaskStatus(SearchTaskStatus.Stopped)) { return; } OnStopSearch(); SearchCallback.ReportComplete(this, SearchResults); } }
/// <summary> /// Override to start the search /// </summary> protected async override void OnStartSearch() { var sortQuery = "relevance"; int pageSize = 40; bool alwaysShowLink = false; var options = StackOverflowQuickLaunchPackage.Instance.OptionPage; if (options != null) { sortQuery = options.Sort.ToString().ToLowerInvariant(); pageSize = options.ShowResults; alwaysShowLink = options.AlwayShowLink; } //// Get the tokens count in the query //uint tokenCount = SearchQuery.GetTokens(0, null); //// Get the tokens //IVsSearchToken[] tokens = new IVsSearchToken[tokenCount]; //SearchQuery.GetTokens(tokenCount, tokens); var cancellationSource = new CancellationTokenSource(); var searchResult = (StackOverflowSearchResult)null; try { using (var client = new HttpClient( new HttpClientHandler { AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate } )) using (var response = await client.GetAsync("https://api.stackexchange.com/2.2/search/excerpts?order=desc&pagesize=" + pageSize + "&sort=" + sortQuery + "&site=stackoverflow&q=" + WebUtility.UrlEncode(SearchQuery.SearchString.Trim()), cancellationSource.Token)) using (var receiveStream = await response.Content.ReadAsStreamAsync()) { DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(StackOverflowSearchResult)); searchResult = serializer.ReadObject(receiveStream) as StackOverflowSearchResult; } } catch (Exception ex) { this.ErrorCode = ex.HResult; this.SetTaskStatus(VSConstants.VsSearchTaskStatus.Error); this.SearchCallback.ReportComplete(this, 0); return; } // Check if the search was canceled if (this.TaskStatus == VSConstants.VsSearchTaskStatus.Stopped) { if (!cancellationSource.IsCancellationRequested) { cancellationSource.Cancel(); if (cancellationSource != null) { cancellationSource.Dispose(); } } // The completion was already notified by the base.OnStopSearch, there is nothing else to do return; } if (searchResult.ErrorId.HasValue) { this.SetTaskStatus(VSConstants.VsSearchTaskStatus.Error); this.SearchCallback.ReportComplete(this, this.SearchResults); return; } bool anyResults = false; if (searchResult != null && searchResult.Items.Length != 0) { anyResults = true; var results = searchResult.Items.Take(pageSize).ToArray(); // Since we know how many items we have, we can report progress for (int itemIndex = 0; itemIndex < results.Length; itemIndex++) { var itemResult = new StackOverflowSearchItemResult( (results[itemIndex].ItemType == ItemType.Question ? "Q: " : "A: ") + WebUtility.HtmlDecode(results[itemIndex].Title), FormatExcerpt(WebUtility.HtmlDecode(results[itemIndex].Excerpt)).Trim(), "https://stackoverflow.com/questions/" + results[itemIndex].QuestionId, new WinFormsIconUIObject(Resources.StackOverflow), searchProvider); // Create and report new result SearchCallback.ReportResult(this, itemResult); // Keep track of how many results we have found, and the base class will use this number when calling the callback to report completion SearchResults++; // Since we know how many items we have, we can report progress SearchCallback.ReportProgress(this, (uint)(itemIndex + 1), (uint)results.Length); } } if (!anyResults || alwaysShowLink) { // Create and report new result SearchCallback.ReportResult(this, new StackOverflowSearchItemResult($"Search Online on Stack Overflow for '{SearchQuery.SearchString}'", string.Empty, "https://stackoverflow.com/search?q=" + WebUtility.UrlEncode(SearchQuery.SearchString.Trim()), null, searchProvider)); // Only one result SearchCallback.ReportComplete(this, 1); } // Now call the base class - it will set the task status to complete and will callback to report search complete base.OnStartSearch(); }
protected override void OnStartSearch() { // Set variables that are used in the finally block. uint resultCount = 0; ErrorCode = VSConstants.S_OK; List <SearchResult> contentItems = null; if (_task != null && (!_task.IsCompleted || _task.Status == Tasks.TaskStatus.Running || _task.Status == Tasks.TaskStatus.WaitingToRun || _task.Status == Tasks.TaskStatus.WaitingForActivation)) { _cancellationTokenSource.Cancel(); } lock (SyncRoot) { _cancellationTokenSource = new CancellationTokenSource(); _task = Tasks.Task.Factory.StartNew(() => { try { // Determine the results. var everythingApiManager = new EverythingApiManager(); _cancellationTokenSource.Token.ThrowIfCancellationRequested(); contentItems = everythingApiManager.Search(SearchQuery.SearchString, _searchBoxInfo, _cancellationTokenSource.Token, CommandPackage.MaxNumberOfResults); resultCount = (uint)contentItems.Count; SearchCallback.ReportComplete(this, resultCount); } catch (Exception e) { ErrorCode = VSConstants.E_FAIL; } finally { ThreadHelper.Generic.Invoke(() => { if (contentItems != null) { var resultListBox = ((SearchBox)_searchWindow.Content).ResultListBox; resultListBox.ItemsSource = contentItems; } }); SearchResults = resultCount; } }, _cancellationTokenSource.Token); try { _task.Wait(_cancellationTokenSource.Token); } catch (OperationCanceledException) { // } } // Call the implementation of this method in the base class. // This sets the task status to complete and reports task completion. base.OnStartSearch(); }
protected override void OnStartSearch() { // Use the original content of the text box as the target of the search. //var separator = new string[] { Environment.NewLine }; //var control = (SqlExplorerControl)m_toolWindow.Content; //string[] contentArr = control.Files.Split(separator, StringSplitOptions.None); //// Get the search option. //bool matchCase = m_toolWindow.MatchCaseOption.Value; //// Set variables that are used in the finally block. //StringBuilder sb = new StringBuilder(""); //uint resultCount = 0; //this.ErrorCode = VSConstants.S_OK; //try //{ // string searchString = this.SearchQuery.SearchString; // // Determine the results. // uint progress = 0; // foreach (string line in contentArr) // { // if (matchCase == true) // { // if (line.Contains(searchString)) // { // sb.AppendLine(line); // resultCount++; // } // } // else // { // if (line.ToLower().Contains(searchString.ToLower())) // { // sb.AppendLine(line); // resultCount++; // } // } // SearchCallback.ReportProgress(this, progress++, (uint)contentArr.GetLength(0)); // } //} //catch (Exception e) //{ // this.ErrorCode = VSConstants.E_FAIL; //} //finally //{ // this.SearchResults = resultCount; //} ErrorCode = VSConstants.S_OK; var filterParameters = new NameFilterParams(SearchQuery.SearchString, _toolWindow.MatchCaseOption.Value, _toolWindow.ShowTables, _toolWindow.ShowProcedures); ThreadHelper.Generic.Invoke(() => { uint resultCount = 0; try { resultCount = _toolWindow.Filter(filterParameters); } catch (Exception) { ErrorCode = VSConstants.E_FAIL; } SearchResults = resultCount; SearchCallback.ReportComplete(this, resultCount); }); // Call the implementation of this method in the base class. // This sets the task status to complete and reports task completion. base.OnStartSearch(); }
protected override void OnStartSearch() { _control.SetFilter(TableToolWindow.SearchFilterKey, new TableSearchFilter(SearchQuery, _control)); SearchCallback.ReportComplete(this, dwResultsFound: 0); }