Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FileSystemWatcher fails with 1000 files creations

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.

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.

Can anyone explain this for me?

like image 569
Linh Avatar asked Dec 15 '10 04:12

Linh


1 Answers

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

Link Excerpt:

Buffer Size and the Error Event

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.

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...

like image 198
Paul Sasik Avatar answered Nov 14 '22 04:11

Paul Sasik