Internally FileSystemWatcher
uses the Windows API ReadDirectoryChangesW
function (as can be seen from the FileSystemWatcher
reference source). The underlying implementation of ReadDirectoryChangesW
is not documented, but in answer to your specific question as to whether FileSystemWatcher
creates separate threads to monitor files, the answer is therefore "no".
It is however worth highlighting the following from the remarks on FileSystemWatcher
in the documentation given that your directories contain many files:
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.
There are two takeaways from this:
- With many
FileSystemWatcher
instances, there will be many buffers created to store the change events, and these are allocated from non-paged memory which is a limited resource.
- If there are a large number of changes taking place in the directories you're monitoring, you may miss events.