M.S.
M.S.

Reputation: 4423

FileSystemWatcher not working even once except for root folder having 320K subdirectories

I am using below code to monitor changes happening in a directory including subdirectories. Currently, I am running three different copies of this application and two of them are working fine. Application for which it is not working contains more than 320K subdirectories in it.

I tried to increase InternalBufferSize but nothing happens. It is working at the root folder and not working for any of the subdirectories.

Also, 2 other copies of the same application monitoring different network paths on different geographical locations are working fine including subdirectories as well.

FileSystemWatcher watcher = new FileSystemWatcher
{
    Path = path,
    IncludeSubdirectories = true,
    Filter = "*.txt",
    NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite |
        NotifyFilters.FileName | NotifyFilters.DirectoryName
};
watcher.Created += Watcher_Created;
watcher.Changed += Watcher_Changed;
watcher.Deleted += Watcher_Deleted;
watcher.Renamed += Watcher_Renamed;
watcher.Error += Watcher_Error;
watcher.EnableRaisingEvents = true;

PS: Polling is not an option. It takes 3 days to scan entirely 320K directories. The root-level folder has 2000 plus subdirectories and each subdirectory has up-to 8 levels of subfolders in it

Edit-1:I have checked with the INFRA team and got to know that shared folders are on EMC Isilon NAS storage.

https://social.msdn.microsoft.com/Forums/vstudio/en-US/38adb37d-bbfc-40d6-8b32-a5c1c7d8d4a3/are-there-any-limitations-of-filesystemwatcher-monitoring-a-unc-path-on-nas-server?forum=netfxbcl

Upvotes: 1

Views: 413

Answers (1)

Theodor Zoulias
Theodor Zoulias

Reputation: 43836

Instead of executing the event handlers synchronously, it may help to offload them to the ThreadPool. This will minimize the risk of overflowing the internal buffer of the FileSystemWatcher. The Offload method below could be used for this purpose:

public static FileSystemEventHandler Offload(FileSystemEventHandler handler)
{
    return (s, e) => ThreadPool.QueueUserWorkItem(_ => handler(s, e));
}

Usage example:

var fsw = new FileSystemWatcher(path);
fsw.Created += Offload((s, e) =>
{
    Console.WriteLine($"Created: {e.Name}");
});

This is not intended as an efficient solution, but just as an easy fix to the buffer-overflow problem (assuming that it's the cause for the issues you are observing).

Upvotes: 3

Related Questions