tags:

views:

515

answers:

4

I'm using .net 2.0 filewatcher to watch a folder for new files. It works perfectly except when I put more than ~80 files at once. The event just doesn't trigger anymore. It's as if the filewatcher is set to keep track of certain number of files.

For the time being I have asked the user not to put more than 50 files at a time and that seems to work however I would like to fix it so that hundreds of files can be dropped into the folder at once.

Here's the code I'm using for the event. It's pretty standard stuff nothing fancy.


FileWatcher = new FileSystemWatcher();
FileWatcher.Path = ConfigurationManager.AppSettings["FolderOfFilesToWatch"];
FileWatcher.NotifyFilter = NotifyFilters.FileName;
FileWatcher.Filter = "*_*_*.*";
FileWatcher.Created += new FileSystemEventHandler(watcher_Created);
FileWatcher.EnableRaisingEvents = true;


static void watcher_Created(object sender, FileSystemEventArgs e)
{
Console.Write(e.Name);
}

Any ideas?

+9  A: 

You probably need to increase the FileSystemWatcher.InternalBufferSize. By default, FileSystemWatcher uses a smaller buffer for performance, and can overflow if too many changes occur in a short time frame.

Try setting a larger buffer size to prevent that from occurring.

Reed Copsey
Just be very careful: as per MSDN: "Increasing buffer size is expensive, as it comes from non paged memory that cannot be swapped out to disk, so keep the buffer as small as possible. To avoid a buffer overflow, use the NotifyFilter and IncludeSubdirectories properties to filter out unwanted change notifications. "
Otávio Décio
@ocdecio: Yes. He's already using NotifyFilter, though, so it would just be a matter of whether or not he's tracking subdirectories. His specific request was to track "hundreds" of files, which will require a buffer size quite a bit larger than the 4k default size.
Reed Copsey
+12  A: 

See Considerations for File Changes on High-Volume Systems.

John Saunders
"By default, the buffer is set to a size of 4 KB. A 4 KB buffer can track changes on approximately 80 files in a directory." Spot-on, nice link.
Andrew Coleson
Thanks for the link.
Tigran
+2  A: 

I use FileWatcher but I took a "belt and suspenders" approach. Whenever I get a FileWatcher event I stick around and check for files I haven't seen before (in my case I have a file catalog). Believe me, there will be a time when you get thousands of files dropped in a folder and you must have another safeguard to account for all of them.

Another alterative you may look into is change journals, although it is (AFAIK) limited to disks attached to your machine, whereas you can use FileWatcher to watch UNC paths as well.

Otávio Décio
+1  A: 

It might be possible that you are exceeding your system's ThreadPool count. When The FileSystemWatcher fires off an event it does so from a thread retrieved from this pool. It is possible lose these events if you don't have enough threads allocated to your ThreadPool.

http://msdn.microsoft.com/en-us/library/system.threading.threadpool.aspx

As others, especially, 'ocdecio' have noted it is good not to completely trust the FileSystemWatcher. It is notoriously inconsistent under heavy load.

William Edmondson