Ejemplo n.º 1
0
        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();
            }
        }
Ejemplo n.º 2
0
        private async void StartButton_Click(object sender, RoutedEventArgs e)
        {
            cancelButton.IsEnabled  = true;
            startButton.IsEnabled   = false;
            cancellationTokenSource = new CancellationTokenSource();

            RegexResultCollection.Clear();
            List <string> lines = InputText.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries).ToList();

            //Set up ProgressBarStatus
            searchProgressBar.Maximum = lines.Count;
            IProgress <ProgressStatus> progress = new Progress <ProgressStatus>(status =>
            {
                searchProgressBar.Value = status.Value;
                progressTextBlock.Text  = status.Text;
            });

            OnlyShowUsedColumns(new Regex(RegexPattern).GetGroupNumbers()?.Length ?? 0);
            await SearchLinesAsync(lines, progress, cancellationTokenSource.Token);
        }