Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FileSystemWatcher does not work properly when many files are added to the directory at the same time

FileSystemWatcher does not work properly when many files are added to the directory at the same time...

The Watcher simply doesn't find all the files in the directory - only if the files are placed in the folder one by one - not if lots of files are copied to the folder at the same time...

Is the creation of Threads the solution to the problem or is there another way to handle the problem ?

like image 781
Henrik Avatar asked Oct 15 '09 13:10

Henrik


People also ask

Which of the following are types of changes that can be detected by the FileSystemWatcher?

The FileSystemWatcher provides us with the event handlers to capture events like renamed, deleted, created and changed.

Is FileSystemWatcher multithreaded?

Nope, filesystemwatchers run on their own thread.

What is the use of FileSystemWatcher class in .NET framework?

Use FileSystemWatcher to watch for changes in a specified directory. You can watch for changes in files and subdirectories of the specified directory. You can create a component to watch files on a local computer, a network drive, or a remote computer.

What is a FileSystemWatcher?

The FileSystemWatcher class in the System.IO namespace can be used to monitor changes to the file system. It watches a file or a directory in your system for changes and triggers events when changes occur. In order for the FileSystemWatcher to work, you should specify a directory that needs to be monitored.


1 Answers

The documentation on that class details that problem:

The Windows operating system notifies your component of file changes in a buffer created by the FileSystemWatcher. If there are many changes in a short time, the buffer can overflow. This causes the component to lose track of changes in the directory, and it will only provide blanket notification. Increasing the size of the buffer with the InternalBufferSize property is expensive, as it comes from non-paged memory that cannot be swapped out to disk, so keep the buffer as small yet large enough to not miss any file change events. To avoid a buffer overflow, use the NotifyFilter and IncludeSubdirectories properties so you can filter out unwanted change notifications.

So, threads probably won't help you much in this case. You probably want to either increase the buffer size (but how large it should be may well depend on the speed of the computer and the disk itself) or constrain what files you are interested in by setting the appropriate filter.

like image 164
Joey Avatar answered Oct 02 '22 05:10

Joey