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 ?
The FileSystemWatcher provides us with the event handlers to capture events like renamed, deleted, created and changed.
Nope, filesystemwatchers run on their own thread.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With