views:

86

answers:

2

We're writing a text editor like tool for our internal accounting package system that has actions that can be done by our own Xml language specs. These macro commands are specified in Xml files and we need the ability to monitor if files openned have bean modified externally.

The only problem is that there maybe 20-30 files with different paths openned at any one time. Would it be good to use multiple FileSystemWatchers for this scenario? Or would it be better to monitor the root drive and catch specific events that match an open file in the editor (though lots of events could be raised).

Some are local drives (C,D,E) others are their network drives (U,X,G,H). Files are quite chunky too about 300-400Kb.

A: 

I'd think that you'd almost certainly need multiple watchers. The FileSystemWatchers buffer can overflow and you could miss events otherwise, you can change the buffer size (using InternalBufferSize) but only up to 64KB.

However, be aware that FileSystemWatcher use FindFirstChangeNotification which is not totally reliable for networked drives (especially if under heavy load, but possibly otherwise too) so you have to expect and plan for that you won't receive every event from the network, and my understanding is that the reliability gets even worse if you've got multiple watchers.

So in summary, I think you'll have to increase the buffers, filter as much as possible and then use as few watchers as you can without overflowing the buffers, so will probably have to be a bit of trial and error to get it right.

ho1
Thanks for the reply, I just realised I haven't logged in too :( Doesn't matter, I'm curious how something like Notepad++ maintains what files are open and if they're modified or not etc.
Jason Crowes
A: 

First a few facts

1- FileSystemWatcher is a wrapper around ReadDirectoryChanges
2- ReadDirectoryChanges creates a kernal buffer to cache event from the non-paged pool memory for each call to ReadDirectoryChanges

The implication of point 2 above is that each instance of FileSystemWatcher will allocate memory from the non-paged pool. Keep in mind that this is the memory used for kernel mode drivers etc. and while it can dynamically expand, it is "hard" limited to a max size calculated at boot time based on the resources of your system (how much memory the machine has).

So given the above, I would consider the following.

If the volume of changes you would expect to see. If this is low, go for multiple watchers with the smallest buffer size you can get away with.

Also keep in mind if you decide to go for big buffers, monitoring network drives does not support buffer sizes over 64K, bigger than that and you will not get the events.

Chris Taylor