Question about FileSystemWatcher behavior

  • Thread starter Thread starter Hardy Wang
  • Start date Start date
H

Hardy Wang

Hi,
I have a FileSystemWatcher object to monitor one certain folder on
server.

FileSystemWatcher watcher = new FileSystemWatcher();
watcher.Path = @"C:\MyFolder";
watcher.NotifyFilter = NotifyFilters.LastWrite | NotifyFilters.FileName |
NotifyFilters.Size;
watcher.Filter = "*.txt";
watcher.Created += new FileSystemEventHandler(watcher_Created);

// Begin watching.
watcher.EnableRaisingEvents = true;

The event is like
private void watcher_Created(object sender, FileSystemEventArgs e) {
// do something lengthy
}

My question is, under heavy situation, if the second file is thrown to this
folder before event handler finishes processing for the first file, will
event handler wait until first one finishes or it will kick out anyway?

Thanks for any suggestion!
 
The FileSystemWatcher events are queued. You can test this behavior by
putting a Thread.Sleep(. . .) in the event handler, and you'll see that
the event handlers operate synchronously. If you have an
subdirectories in c:\MyFolder, note that since you are watching for
LastWrite, you will have an event to handle when any files inside the
subfolder changes because that subfolder's LastWrite time will change.

Note that there is a buffer that queues file changing events, so if
there are a lot of changes (hundreds), the buffere will overflow and
corrupt your watcher, and you'll have to recreate it. This isn't a
problem if you won't have hundreds of file changes in a short period of
time.

Best regards,
Jeffrey Palermo
Blog: http://www.jeffreypalermo.com
 
Back
Top