Browse Source

Improve error handling in SearchForIssuesCommand.

newNRvisualizers
Daniel Grunwald 13 years ago
parent
commit
2f8e356c77
  1. 45
      src/AddIns/BackendBindings/CSharpBinding/Project/Src/Refactoring/SearchForIssuesCommand.cs

45
src/AddIns/BackendBindings/CSharpBinding/Project/Src/Refactoring/SearchForIssuesCommand.cs

@ -8,6 +8,7 @@ using System.IO;
using System.Linq; using System.Linq;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Windows.Input;
using CSharpBinding.Parser; using CSharpBinding.Parser;
using ICSharpCode.AvalonEdit.Document; using ICSharpCode.AvalonEdit.Document;
using ICSharpCode.AvalonEdit.Highlighting; using ICSharpCode.AvalonEdit.Highlighting;
@ -47,6 +48,15 @@ namespace CSharpBinding.Refactoring
fixedIssueCount, remainingIssues.Count); fixedIssueCount, remainingIssues.Count);
SearchResultsPad.Instance.ShowSearchResults(title, remainingIssues); SearchResultsPad.Instance.ShowSearchResults(title, remainingIssues);
MessageService.ShowMessage(message, title); MessageService.ShowMessage(message, title);
} else if (Keyboard.Modifiers == (ModifierKeys.Control | ModifierKeys.Alt | ModifierKeys.Shift)) {
// Ctrl+Alt+Shift => run issue search on main thread,
// this helps debugging as exceptions don't get caught and passed from one thread to another
List<SearchResultMatch> issues = new List<SearchResultMatch>();
AsynchronousWaitDialog.RunInCancellableWaitDialog(
title, null,
monitor => SearchForIssues(fileNames, providers, f => issues.AddRange(f.Matches), monitor)
);
SearchResultsPad.Instance.ShowSearchResults(title, issues);
} else { } else {
var monitor = SD.StatusBar.CreateProgressMonitor(); var monitor = SD.StatusBar.CreateProgressMonitor();
var observable = ReactiveExtensions.CreateObservable<SearchedFile>( var observable = ReactiveExtensions.CreateObservable<SearchedFile>(
@ -92,25 +102,50 @@ namespace CSharpBinding.Refactoring
Task SearchForIssuesAsync(List<FileName> fileNames, IEnumerable<IssueManager.IssueProvider> providers, Action<SearchedFile> callback, IProgressMonitor monitor) Task SearchForIssuesAsync(List<FileName> fileNames, IEnumerable<IssueManager.IssueProvider> providers, Action<SearchedFile> callback, IProgressMonitor monitor)
{ {
return Task.Run(() => SearchForIssues(fileNames, providers, callback, monitor)); return Task.Run(() => SearchForIssuesParallel(fileNames, providers, callback, monitor));
} }
void SearchForIssues(List<FileName> fileNames, IEnumerable<IssueManager.IssueProvider> providers, Action<SearchedFile> callback, IProgressMonitor monitor) void SearchForIssuesParallel(List<FileName> fileNames, IEnumerable<IssueManager.IssueProvider> providers, Action<SearchedFile> callback, IProgressMonitor monitor)
{ {
ParseableFileContentFinder contentFinder = new ParseableFileContentFinder(); ParseableFileContentFinder contentFinder = new ParseableFileContentFinder();
int filesProcessed = 0; int filesProcessed = 0;
Parallel.ForEach( Parallel.ForEach(
fileNames, fileNames,
new ParallelOptions {
MaxDegreeOfParallelism = Environment.ProcessorCount,
CancellationToken = monitor.CancellationToken
},
delegate (FileName fileName) { delegate (FileName fileName) {
var fileContent = contentFinder.Create(fileName); var fileContent = contentFinder.Create(fileName);
var resultForFile = SearchForIssues(fileName, fileContent, providers, monitor.CancellationToken); try {
if (resultForFile != null) { var resultForFile = SearchForIssues(fileName, fileContent, providers, monitor.CancellationToken);
callback(resultForFile); if (resultForFile != null) {
callback(resultForFile);
}
} catch (OperationCanceledException) {
throw;
} catch (Exception ex) {
throw new ApplicationException("Exception while searching for issues in " + fileName, ex);
} }
monitor.Progress = (double)Interlocked.Increment(ref filesProcessed) / fileNames.Count; monitor.Progress = (double)Interlocked.Increment(ref filesProcessed) / fileNames.Count;
}); });
} }
void SearchForIssues(List<FileName> fileNames, IEnumerable<IssueManager.IssueProvider> providers, Action<SearchedFile> callback, IProgressMonitor monitor)
{
// used for debugging so that we can break in the crashing issue
ParseableFileContentFinder contentFinder = new ParseableFileContentFinder();
int filesProcessed = 0;
foreach (FileName fileName in fileNames) {
var fileContent = contentFinder.Create(fileName);
var resultForFile = SearchForIssues(fileName, fileContent, providers, monitor.CancellationToken);
if (resultForFile != null) {
callback(resultForFile);
}
monitor.Progress = (double)(++filesProcessed) / fileNames.Count;
}
}
SearchedFile SearchForIssues(FileName fileName, ITextSource fileContent, IEnumerable<IssueManager.IssueProvider> providers, CancellationToken cancellationToken) SearchedFile SearchForIssues(FileName fileName, ITextSource fileContent, IEnumerable<IssueManager.IssueProvider> providers, CancellationToken cancellationToken)
{ {
cancellationToken.ThrowIfCancellationRequested(); cancellationToken.ThrowIfCancellationRequested();

Loading…
Cancel
Save