Пример #1
0
 public LineCountProgress CopyTo(LineCountProgress copy)
 {
     copy.FileEvent = FileEvent;
     copy.Count     = Count;
     copy.Status    = Status;
     copy.Note      = Note;
     return(copy);
 }
Пример #2
0
        void CountAndReport(FileEvent fileEvent, IProgress <LineCountProgress> progress)
        {
            LineCountProgress report = Count(fileEvent);

            if (progress != null)
            {
                progress.Report(report);
            }
        }
Пример #3
0
        public LineCountProgress Count(FileEvent fileEvent)
        {
            const double TimeoutSeconds = 5.0;
            const int    RetryInterval  = 900; // milliseconds
            var          report         = new LineCountProgress(fileEvent);

            DateTime startTime  = DateTime.Now;
            TimeSpan elapsed    = new TimeSpan(ticks: 0);
            bool     keepTrying = true;

            while (keepTrying)
            {
                try
                {
                    int    count = FileEntry.NotCounted;
                    string path  = Path.Combine(FolderPath ?? "", fileEvent.FileEntry.Name);

                    using (var f = new StreamReader(path, detectEncodingFromByteOrderMarks: true))
                    {
                        string line;
                        do
                        {
                            count++;
                            line = f.ReadLine();
                        }while (line != null);
                    }
                    report.Count  = count;
                    report.Status = LineCountStatus.Success;
                    keepTrying    = false;
                }
                catch (IOException ex)
                {
                    if (ex is DirectoryNotFoundException ||
                        ex is DriveNotFoundException ||
                        ex is FileNotFoundException ||
                        ex is PathTooLongException)
                    {
                        report.Status = LineCountStatus.FileNotFound;
                        report.Note   = ex.Message;
                        keepTrying    = false;
                    }
                    else
                    {
                        // locked file raises IOException base class
                        elapsed = DateTime.Now - startTime;
                        if (elapsed.TotalSeconds >= TimeoutSeconds)
                        {
                            report.Status = LineCountStatus.TimedOut;
                            report.Note   = "Timed out after " + elapsed;
                            keepTrying    = false;
                        }
                        else
                        {
                            Thread.Sleep(RetryInterval);
                        }
                    }
                }
                catch (Exception ex)
                {
                    report.Status = LineCountStatus.Exception;
                    report.Note   = ex.Message;
                    keepTrying    = false;
                }
            }

            return(report);
        }
Пример #4
0
        void CountAndSave(FileEvent fileEvent, List <LineCountProgress> results)
        {
            LineCountProgress report = Count(fileEvent);

            results.Add(report);
        }