private async Task SearchLinesAsync(IList <string> lines, IProgress <ProgressStatus> progress, CancellationToken token) { try { //Reset progress progress.Report(new ProgressStatus(0, "Matches: 0")); int matchCount = 0; Match currentMatch; IEnumerable <string> groups; Regex regex = new Regex(RegexPattern, GetRegexOptions()); if (ReadLineByLine) { for (int i = 0; i < lines.Count(); i++) { //Cancellation token.ThrowIfCancellationRequested(); currentMatch = regex.Match(lines[i]); if (currentMatch.Success) { //Fake delay await Task.Delay(TimeSpan.FromSeconds(0.5)); //Add matches/groups and line to collection groups = currentMatch.Groups.OfType <Group>().Select(g => g.Value); RegexResultCollection.Add(new RegexResult(++matchCount, lines[i], groups)); } progress.Report(new ProgressStatus(i + 1, "Matches: " + matchCount)); } } else { MatchCollection matches = regex.Matches(InputText); for (int i = 0; i < matches.Count; i++) { //Add matches/groups to collection groups = matches[i].Groups.OfType <Group>().Select(g => g.Value); RegexResultCollection.Add(new RegexResult(++matchCount, string.Empty, groups)); } progress.Report(new ProgressStatus(lines.Count, "Matches: " + matchCount)); } } catch (Exception) { //Ignore cancellation } finally { cancelButton.IsEnabled = false; SetStartButtonStatus(); } }