文件系统观察程序仅检查完整文件(File System Watcher only checks complete files)

我正在建立一个监视图像目录然后处理它们的系统。

有时可以将PDF放入监视目录中,在这种情况下,我将它们转换为图像并继续正常运行。

但是,一旦处理完图像,它就会将其移动到一个完整的文件夹中,但如果PDF到图像的转换没有完成,那么我的代码会抛出一个IO异常,因为它无法移动另一个进程正在使用的文件。

是否可以指定通知过滤器仅处理“完整”文件。 完成后我的意思是它已完成复制,移动或创建。

我猜这种冲突的发生是因为处理文件的工作者正在不同的线程上运行。

public void Start() { if (string.IsNullOrWhiteSpace(this.fileDirectory)) { throw new Exception("No file directory specified."); } // watch for any type of file activity const NotifyFilters Filters = NotifyFilters.LastAccess | NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.DirectoryName | NotifyFilters.CreationTime; // set up the watcher based on the app.config file var watcher = new FileSystemWatcher() { Path = this.fileDirectory, NotifyFilter = Filters, EnableRaisingEvents = true }; // event handler for new files watcher.Created += this.NewFileCreated; // file system watcher doesn't "see" current files, manually get these this.ProcessExistingFiles(); } private void NewFileCreated(object source, FileSystemEventArgs e) { Task.Run(() => this.ProcessFile(e.FullPath)); } private void ProcessExistingFiles() { var files = Directory.GetFiles(this.fileDirectory); Parallel.ForEach(files, this.ProcessFile); }

I'm setting up a system which monitors a directory for images and then processes them.

From time to time PDFs may be dropped into the watched directory, in this case I convert them to an image and continue as normal.

However once the image has been processed it moves it into a complete folder but if the PDF to image conversion isn't complete then my code throws an IO Exception because it can't move a file which is being used by another process.

Is it possible when specifying the notify filters to only process "complete" files. By complete I mean it has finished copying, moving, or being created.

I am guessing that this conflict occurs because the worker than handles the files is running on different threads.

public void Start() { if (string.IsNullOrWhiteSpace(this.fileDirectory)) { throw new Exception("No file directory specified."); } // watch for any type of file activity const NotifyFilters Filters = NotifyFilters.LastAccess | NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.DirectoryName | NotifyFilters.CreationTime; // set up the watcher based on the app.config file var watcher = new FileSystemWatcher() { Path = this.fileDirectory, NotifyFilter = Filters, EnableRaisingEvents = true }; // event handler for new files watcher.Created += this.NewFileCreated; // file system watcher doesn't "see" current files, manually get these this.ProcessExistingFiles(); } private void NewFileCreated(object source, FileSystemEventArgs e) { Task.Run(() => this.ProcessFile(e.FullPath)); } private void ProcessExistingFiles() { var files = Directory.GetFiles(this.fileDirectory); Parallel.ForEach(files, this.ProcessFile); }

最满意答案

根据我的理解,您有两种类型的文件到达受监控的目录:images和pdf。 如果pdf到达,您必须先将其转换为图像。 我不确定你描述的例外的原因是什么 - 转换和处理(最后移动)同时完成?

如果不是 - 你看错了地方。 可能是将文件置于首位的进程仍然保持着它的句柄。

如果答案是肯定的,那么我有两个建议:

将pdf文件放入另一个文件夹。 转换过程将侦听此文件夹,并在提取图像后立即进行移动。 这样,您无需更改当前算法中的任何内容,只需添加另一个隔离级别即可。

或者,您可以在FSW上设置过滤器以仅监视图像文件(过滤器文件扩展名),当将pdf放入目录时,它不会立即进行处理,而是首先转换为图像。 在转换结束时,受监控的目录突然“接收”另一个图像! 这个过程继续正常进行。 这实际上是另一种在图像和pdf之间接收隔离的方法。

From what I understand, you have files two types of files that arrive to a monitored directory: images and pdf's. If a pdf arrives, you must first convert it to an image. I am not sure what is the reason for the exception you describe- is the conversion and the processing (with moving in the end) done simultaneously?

If not- you are looking in the wrong place. It may be that the process that put the file there in the first place is still holding a handle to it.

If the answer is yes, then I have two suggestions:

Put the pdf files into a different folder. The conversion process will listen to this folder and will make the move as soon as the images are extracted. This way, you do not need to change anything in the current algorithm, just add another isolation level.

Alternatively, you can set a filter on the FSW to monitor only image files (filter file extension) this way, when a pdf is put to the directory, it does not go straight away to processing, it first gets converted to an image. At the end of the conversion, the monitored directory suddenly "receives" another another image! And the process continues as normal. This is essentially another way to receive isolation between the images and the pdfs.

更多推荐