在结束由FSW找到的新文件后,Optimizer不会停止

问题描述 投票:0回答:1

我已经创建了一个CLI应用程序,它将监视目录并优化移入目录的所有新PDF。截至我上次构建时没有错误。

我遇到的问题是,当我运行它时,应用程序将检测到更改并优化已更改的文件,但它不会停止优化新文件的周期。

一旦达到新文件的末尾,我将如何在优化过程中设置一个停止点?

public class Methods
        {
            [PermissionSet(SecurityAction.Demand, Name = "FullTrust")]
            public static void Optimize()
            {
                Thread.Sleep(1000);
                PDFNet.Initialize();

                string input_Path = @"C:\Users\user\Desktop\testinpactive\";
                string output_Path = @"C:\Users\user\Desktop\output\";
                string[] files = Directory.GetFiles(input_Path, "*.pdf", SearchOption.AllDirectories);

                foreach (string file in files)
                {
                    string fileName = Path.GetFileName(file);
                    Console.WriteLine($"Optimizing {fileName}");
                    string sub = file.Substring(41, 7);
                    CreateFolder(output_Path + sub);

                    // CreateFolder(output_Path + );
                    try
                    {
                        using (PDFDoc doc = new PDFDoc(file))
                        {
                            //--------------------------------------------------------------------------------
                            // Example 1) Simple optimization of a pdf with default settings.
                            doc.InitSecurityHandler();
                            Optimizer.Optimize(doc);
                            doc.Save(output_Path + sub + fileName, SDFDoc.SaveOptions.e_linearized);
                            // File Delete Process
                            //File.Delete(input_Path + files);
                            //Console.WriteLine("File Deleted");
                            Console.WriteLine("Done..\n");
                        }
                    }
                    catch (PDFNetException e)
                    {
                        Console.WriteLine(e.Message);
                    }
                }
            }

            public static void Run()
            {
                // Create a new FileSystemWatcher and set its properties.
                // Params: Path, and filter
                using (FileSystemWatcher watcher = new FileSystemWatcher(@"C:\Users\user\Desktop\testinpactive", "*.pdf"))
                {
                    // To watch SubDirectories 
                    watcher.IncludeSubdirectories = true;

                    FswHandler Handler = new FswHandler();

                    // Add event handlers.
                    watcher.Created += Handler.OnCreated;

                    // Begin watching.
                    watcher.EnableRaisingEvents = true;

                    // Wait for the user to quit the program.
                    Console.WriteLine("Press 'q' to quit the sample.");
                    while (Console.Read() != 'q');
                }
            }
            public class FswHandler
            {
                public void OnCreated(Object source, FileSystemEventArgs e)
                {
                    // Write out Path (Testing)
                    Console.WriteLine($"FILE: {e.FullPath} CHANGE-TYPE: {e.ChangeType}");
                    // Specify what is done when a file is changed, created, or deleted.
                    Optimize();
                }

            }
c# filesystemwatcher
1个回答
0
投票

非常感谢@BenVoigt解释了我做错了。

© www.soinside.com 2019 - 2024. All rights reserved.