Beispiel #1
0
        public void Run(CancellationToken cancellationToken)
        {
            lock (_busy)
            {
                if (Immutable) return;

                if (_state != MicroTaskStates.None) _log.Error("_state != MicroTaskStates.None");

                if (cancellationToken.IsCancellationRequested) return;
                State = MicroTaskStates.Reading;
                var text = _file.ReadText();
                var replacer = new TextReplacer(text);

                if (cancellationToken.IsCancellationRequested) return;
                State = MicroTaskStates.Searching;
                var matches = new List<RelatedMatch>();
                var m = _regex.RelatedMatch(text, 0, replacer);
                while (m.Success)
                {
                    CurrentStagePercentage = (double)(m.StartIndex + m.Length) / text.Length;
                    matches.Add(m);
                    if (cancellationToken.IsCancellationRequested) return;
                    m = _regex.RelatedMatch(text, m.StartIndex + m.Length, replacer);
                }
                if (matches.Count == 0) matches.Add(m);

                if (!matches[0].Success)
                {
                    Immutable = true;
                    State = MicroTaskStates.Complete;
                    return;
                }

                if (cancellationToken.IsCancellationRequested) return;
                State = MicroTaskStates.Replacing;
                foreach (var relatedMatch in matches)
                    replacer.Replace(relatedMatch, _replacement.CreateCopyWithGroups(relatedMatch));

                State = MicroTaskStates.BuildingResult;
                var result = replacer.BuildResult();
                ReplacesCount += matches.Count;

                if (cancellationToken.IsCancellationRequested) return;
                State = MicroTaskStates.SavingResult;
                _file.WriteText(result);

                State = MicroTaskStates.Complete;
            }
        }
Beispiel #2
0
 public void Cancel()
 {
     lock (_busy)
     {
         _log.Debug("afterlock, State=" + State);
         if (State == MicroTaskStates.Complete)
             _file.RestoreBackup();
         State = MicroTaskStates.None;
     }
 }