public void ProcessImage(CancellationToken token) { int currentImageIndex = -1; int imageCount = 0; bool isNextPageWaiting = false; PdfUtility pdf = new PdfUtility(); do { foreach (var file in FileSystem.InputDirectory.GetFiles().Skip(imageCount)) { string fileName = file.Name; bool isValid = Regex.IsMatch(fileName, @"^img_[0-9]{3}.(jpg|png|jpeg)$"); if (isValid) { int imageIndex = GetImageIndex(fileName); if (imageIndex != currentImageIndex + 1 && currentImageIndex != -1 && isNextPageWaiting) { Save(ref pdf, out isNextPageWaiting); } if (FileSystem.TryOpenFile(file, 3)) { pdf.AddImage(file.FullName); imageCount++; currentImageIndex = imageIndex; isNextPageWaiting = true; } } else { string outFile = Path.Combine(FileSystem.TempDirectory.FullName, fileName); if (File.Exists(outFile)) { FileSystem.DeleteFile(file); } else { file.MoveTo(outFile); } } } if (!_newFileEvent.WaitOne(5000) && isNextPageWaiting) { Save(ref pdf, out isNextPageWaiting); } if (token.IsCancellationRequested) { if (isNextPageWaiting) { pdf.Save(FileSystem.GetNextFilename()); } foreach (var file in FileSystem.InputDirectory.GetFiles()) { FileSystem.DeleteFile(file); } } } while (!token.IsCancellationRequested); }
private void Save(ref PdfUtility pdf, out bool isNextPageWaiting) { pdf.Save(FileSystem.GetNextFilename()); pdf = new PdfUtility(); isNextPageWaiting = false; }