FileSystemWatcher problem - multiple hits

  • Thread starter Thread starter zfeld
  • Start date Start date
Z

zfeld

I am serializing an object to XML and writing it to disk. whenever a change
to my object occurs I call the save function to re-write to disk (see code
below). I am monitoring the directory where this write is being done using a
FileSystemWatcher.

My problem is that my Watcher.Changed callback is always invoked twice.This
is causing me to process the change twice (causing a performance hit).

I am filtering to only be notified for Watcher.NotifyFilter =
NotifyFilters.DirectoryName | NotifyFilters.LastWrite; and I still get
called twice During debugging I check the path of the changed file it is the
same exact .xml file each time.

Why is this happening? Am I some how causing 2 writes that are causing the 2
notifications (see code below)? This causing me tremendous problems. Are
there any idea for a hack that I can ignore the duplicate write? I can't
just ignore any subsequent write being that it might be a new change and the
file sizes might still be the same.

/// <summary>
/// Saves the data object to XML on disk.
/// </summary>
/// <param name="Path">File name to write to</param>
public bool Save(string Path) {
try{
XmlSerializer XMLFormatter = new XmlSerializer(this.GetType());
Stream File = new FileStream(Path, FileMode.Create,
FileAccess.Write, FileShare.None);
XMLFormatter.Serialize(File, this);
File.Close();
return true;
}
catch(Exception e){
Type exceptionType = e.GetType();
return false;
}
catch{
return false;
}
}


///filtering code

Watcher.NotifyFilter = NotifyFilters.DirectoryName |
NotifyFilters.LastWrite;
Watcher.Filter = ""; // watch all files
Watcher.IncludeSubdirectories = true; // look at all subdirectories
Watcher.EnableRaisingEvents = true; // fire off the watcher
 
Okay so you are doing a file.save when you get a file changed event? I am
kinda surprised why you are getting only two hits, this should go into an
infinite loop.
The way to get around this would be to disable the filesystem watcher just
before save, and re-enable right after save.

- Sahil Malik
http://dotnetjunkies.com/weblog/sahilmalik
 
No, No, No, I don't save when the file is changed. That would be an obvious
infinite loop. When the user does something that changes some attribute to
the object I then save the change to disk. I then listen to the file change
to update some GUIs which do not trigger a new save. But these Gui changes
are being invoked twice.
 
this might not be a great idea but if you see Change event happens with same
time stamp. what i do it check it if it is same. in my condition this
solution is ok as i am sure that only 1 file will be accesses every second
but this might not be a good solution for you if there are many access in a
second.
 
Thanks, this might not be the best idea, but I will probably just go ahead
and do that.
The bigger question is what exactly is triggering these 2 notifications.
 
zfeld said:
I am serializing an object to XML and writing it to disk. whenever a
change to my object occurs I call the save function to re-write to
disk (see code below). I am monitoring the directory where this write
is being done using a FileSystemWatcher.

My problem is that my Watcher.Changed callback is always invoked
twice.This is causing me to process the change twice (causing a
performance hit).

What are you using to edit the XML file?

Its a 'feature' of notepad.exe that the file is touched twice when it saves
a file, hence FileSystemWatcher will raise two changed events.

Richard
 
Back
Top