FileSystemWatcher的失败,1000个文件创作文件、FileSystemWatcher

2023-09-04 10:21:17 作者:彼此都那么的执着。

我使用FileSystemWatcher的如果创建或复制到该文件夹​​中的文件,以监视文件夹A,它将会被移动到文件夹B。

I'm using FileSystemWatcher to monitor folder A. if a file is created or copied into that folder, it will be moved to folder B.

我创建的文件夹中的文件通过右键单击,选择新建,选择文本文档。该文件被移动到立即文件夹B。但是,如果我用一个程序来创建1000个文件到文件夹中的那么它们不会被复制到文件夹B.由于这似乎FileSystemWatcher对象无法检测到1000个文件是由程序创建。

I created a file in folder A by right clicking, selecting "New", select "Text Document". The file is moved to folder B immediately. But if I use a program to create 1000 files into folder A then they are not moved to folder B. As it would seem FileSystemWatcher can't detect 1000 files were created by program.

谁能解释这对我?

推荐答案

看起来像一个1000文件将溢出FileSystemWatcher的缓冲区。这里大解释。

Looks like a 1000 files will overrun the FileSystemWatcher's buffer. Great explanation here.

链接摘录:

缓冲区大小和错误事件

在FileSystemWatcher类的工作原理   捕获的所有相关文件和   年龄较大的变化,并把它们变成   缓冲。这是再处理的一项   改变一次,直到所有的   通知进行了处理,并   缓冲区是空的。默认情况下,   内部缓冲区有八个大小   千字节(8192字节)。每一个事件可以   最多需要16个字节的缓冲器的   为它的数据,不包括文件   名称。这就是说,当有一   很多变化在短期内   时间,缓冲器可以迅速成为   超载和通知可以   丢失了。

The FileSystemWatcher class works by capturing all of the relevant file and older changes and placing them into a buffer. This is then processed one change at a time until all of the notifications have been dealt with and the buffer is empty. By default, the internal buffer has a size of eight kilobytes (8192 bytes). Each event can take up to sixteen bytes of the buffer for its data, not including the file name. This means that when there are a lot of changes in a short period of time, the buffer can quickly become overloaded and notifications can be lost.

从FileSystemWatcher的处理正确的错误事件将让你code为这种可能性。例如。检查和/或在错误事件的文件夹,用自己的逻辑操作...

Handling the proper error event from FileSystemWatcher will allow you to code for this possibility. E.g. Examine and/or operate on the error event's folder with your own logic...