diff options
Diffstat (limited to 'main/src/core/MonoDevelop.Ide/MonoDevelop.Ide.FindInFiles/FindInFilesDialog.cs')
-rw-r--r-- | main/src/core/MonoDevelop.Ide/MonoDevelop.Ide.FindInFiles/FindInFilesDialog.cs | 103 |
1 files changed, 49 insertions, 54 deletions
diff --git a/main/src/core/MonoDevelop.Ide/MonoDevelop.Ide.FindInFiles/FindInFilesDialog.cs b/main/src/core/MonoDevelop.Ide/MonoDevelop.Ide.FindInFiles/FindInFilesDialog.cs index 8ddfbccf09..136bb723de 100644 --- a/main/src/core/MonoDevelop.Ide/MonoDevelop.Ide.FindInFiles/FindInFilesDialog.cs +++ b/main/src/core/MonoDevelop.Ide/MonoDevelop.Ide.FindInFiles/FindInFilesDialog.cs @@ -735,7 +735,7 @@ namespace MonoDevelop.Ide.FindInFiles SearchReplace (comboboxentryFind.Entry.Text, null, GetScope (), GetFilterOptions (), () => UpdateStopButton ()); } - readonly static List<ISearchProgressMonitor> searchesInProgress = new List<ISearchProgressMonitor> (); + readonly static List<CancellationTokenSource> searchesInProgress = new List<CancellationTokenSource> (); void UpdateStopButton () { buttonStop.Sensitive = searchesInProgress.Count > 0; @@ -746,19 +746,19 @@ namespace MonoDevelop.Ide.FindInFiles lock (searchesInProgress) { if (searchesInProgress.Count == 0) return; - ISearchProgressMonitor monitor = searchesInProgress[searchesInProgress.Count - 1]; - monitor.AsyncOperation.Cancel (); + var cs = searchesInProgress[searchesInProgress.Count - 1]; + cs.Cancel (); } } - internal static void SearchReplace (string findPattern, string replacePattern, Scope scope, FilterOptions options, System.Action UpdateStopButton) + internal async static void SearchReplace (string findPattern, string replacePattern, Scope scope, FilterOptions options, System.Action UpdateStopButton) { if (find != null && find.IsRunning) { if (!MessageService.Confirm (GettextCatalog.GetString ("There is a search already in progress. Do you want to stop it?"), AlertButton.Stop)) return; lock (searchesInProgress) { - foreach (var mon in searchesInProgress) - mon.AsyncOperation.Cancel (); + foreach (var cs in searchesInProgress) + cs.Cancel (); searchesInProgress.Clear (); } } @@ -779,57 +779,52 @@ namespace MonoDevelop.Ide.FindInFiles return; } - ThreadPool.QueueUserWorkItem (delegate { - using (ISearchProgressMonitor searchMonitor = IdeApp.Workbench.ProgressMonitors.GetSearchProgressMonitor (true)) { - searchMonitor.ReportStatus (scope.GetDescription (options, pattern, null)); - - lock (searchesInProgress) - searchesInProgress.Add (searchMonitor); - if (UpdateStopButton != null) { - Application.Invoke (delegate { - UpdateStopButton (); - }); + using (SearchProgressMonitor searchMonitor = IdeApp.Workbench.ProgressMonitors.GetSearchProgressMonitor (true)) { + searchMonitor.ReportStatus (scope.GetDescription (options, pattern, null)); + var cs = CancellationTokenSource.CreateLinkedTokenSource (searchMonitor.CancellationToken); + lock (searchesInProgress) + searchesInProgress.Add (cs); + if (UpdateStopButton != null) + UpdateStopButton (); + + DateTime timer = DateTime.Now; + string errorMessage = null; + + try { + ResultQueue<SearchResult> results = new ResultQueue<SearchResult> (); + find.FindAll (scope, searchMonitor, pattern, replacePattern, options, results); + SearchResult[] res = await results.DequeueMany (); + while (res.Length > 0) { + if (cs.IsCancellationRequested) + return; + searchMonitor.ReportResults (res); + res = await results.DequeueMany (); } - - DateTime timer = DateTime.Now; - string errorMessage = null; - - try { - var results = new List<SearchResult> (); - foreach (SearchResult result in find.FindAll (scope, searchMonitor, pattern, replacePattern, options)) { - if (searchMonitor.IsCancelRequested) - return; - results.Add (result); - } - searchMonitor.ReportResults (results); - } catch (Exception ex) { - errorMessage = ex.Message; - LoggingService.LogError ("Error while search", ex); - } - - string message; - if (errorMessage != null) { - message = GettextCatalog.GetString ("The search could not be finished: {0}", errorMessage); - searchMonitor.ReportError (message, null); - } else if (searchMonitor.IsCancelRequested) { - message = GettextCatalog.GetString ("Search cancelled."); - searchMonitor.ReportWarning (message); - } else { - string matches = string.Format (GettextCatalog.GetPluralString ("{0} match found", "{0} matches found", find.FoundMatchesCount), find.FoundMatchesCount); - string files = string.Format (GettextCatalog.GetPluralString ("in {0} file.", "in {0} files.", find.SearchedFilesCount), find.SearchedFilesCount); - message = GettextCatalog.GetString ("Search completed.") + Environment.NewLine + matches + " " + files; - searchMonitor.ReportSuccess (message); - } - searchMonitor.ReportStatus (message); - searchMonitor.Log.WriteLine (GettextCatalog.GetString ("Search time: {0} seconds."), (DateTime.Now - timer).TotalSeconds); - searchesInProgress.Remove (searchMonitor); + } catch (Exception ex) { + errorMessage = ex.Message; + LoggingService.LogError ("Error while search", ex); } - if (UpdateStopButton != null) { - Application.Invoke (delegate { - UpdateStopButton (); - }); + + string message; + if (errorMessage != null) { + message = GettextCatalog.GetString ("The search could not be finished: {0}", errorMessage); + searchMonitor.ReportError (message, null); + } else if (cs.IsCancellationRequested) { + message = GettextCatalog.GetString ("Search cancelled."); + searchMonitor.ReportWarning (message); + } else { + string matches = string.Format (GettextCatalog.GetPluralString ("{0} match found", "{0} matches found", find.FoundMatchesCount), find.FoundMatchesCount); + string files = string.Format (GettextCatalog.GetPluralString ("in {0} file.", "in {0} files.", find.SearchedFilesCount), find.SearchedFilesCount); + message = GettextCatalog.GetString ("Search completed.") + Environment.NewLine + matches + " " + files; + searchMonitor.ReportSuccess (message); } - }); + searchMonitor.ReportStatus (message); + searchMonitor.Log.WriteLine (GettextCatalog.GetString ("Search time: {0} seconds."), (DateTime.Now - timer).TotalSeconds); + searchesInProgress.Remove (cs); + } + if (UpdateStopButton != null) { + UpdateStopButton (); + } } } } |