views:

93

answers:

5

I am wondering whether watching a file/directory for changes using the FileSystemWatcher class is extremely memory intensive. I am developing a desktop application in C# that will be running behind the scenes continuously on low-performance computers, and I need some way of checking to see if various files have changed. I can think of a few solutions:

  1. Watch the directories using FileSystemWatcher.
  2. Run a timed thread on an interval that goes through and manually checks this.
  3. Check manually every time the actionhandler thread runs (the program will occasionally do something, on an action).

Any suggestions?

Thanks!

badPanda

+6  A: 

Event-based are always more efficient than polling using the same or spawning a new thread, so it's the right idea to use FileSystemWatcher. The programming model, which uses delegates is also more elegant in my opinion.

Khnle
True, but don't forget that some events may slip through when using a FileSystemWatcher (see http://msdn.microsoft.com/en-us/library/system.io.filesystemwatcher.aspx: "If there are many changes in a short time, the buffer can overflow"). If you want to be sure that you don't miss anything you will have to do additional polling (at a low rate).
0xA3
Also note there is NO WAY to avoid this overflow behavior in your code; it is a buffer that is in no way related to properties of the FileSystemWatcher you create. A lot of changes in a short amount of time (we've seen it with as few as 150 file pastes/copies) will result in missed events with no notification that events were missed.
Joe
Hmm. I am aware of the buffer overflow issue and am not concerned for two reasons: the first being that the likelihood of more than 150 changes to this directory is very small (I would say it is a once a year scenario). The second is that I have written the code to simply need to be aware that a change has occurred; more filtering of the results is required anyways so as long as the buffer overflow is handled I do not think it will be an issue.
badpanda
*the likelihood of more than 150 changes in between calls to the OnChange handler is very small.
badpanda
@badpanda: I think it's an extreme scenario to have the buffer overflow. We never had anything remotely near that extreme and sounds like you don't either.
Khnle
Certainly. Also, for my purposes I am able to simply catch all exceptions arising from that block of code, assume that if there is one it is a buffer overflow (I cannot anticipate any others), and perform my task anyways, since it is not necessary for me to know which files changed.
badpanda
+3  A: 

I used FileSystemWatcher class to watch *.txt file changes within hundreds of directories with no perfomance issues. So I guess it's the best solution.

Max
+2  A: 

You want to go with FileSystemWatcher. It is what this object is designed to do. It has been developed, and tested for widespread use. Anything that you write will not go under the scrutiny of testing and optimization that it has. There's no reason to re-invent the wheel when you don't have to.

Kevin
Except FileSystemWatcher doesn't always work and can't be avoided: http://social.msdn.microsoft.com/Forums/en-US/netfxbcl/thread/4465cafb-f4ed-434f-89d8-c85ced6ffaa8
Joe
+1  A: 

In a large scale file transfer project I am working on currently (nation wide), FileSystemWatcher was a one way option (with some management on file locks, events queueing, etc). You have so many advantages, on using the underlying OS constructs (providing all this event based interaction), that I don't think that you will achieve such efficient behaviour otherwise.

Aggelos Mpimpoudis
+1  A: 

I've seen a few programs which check the file/directory manually for changes every time the program regains focus. This may be appropriate if your program meets a few requirements:

  1. It is common for your program to be inactive (i.e. in a state where knowing about changes to files is unnecessary) for long periods of time.
  2. It is unlikely for file/directory to change while the program is active (except by the program itself, which does not require notification).
  3. You are only concerned with a small number of files.

Even so, FileSystemWatcher is fine even in that case.

Brian