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
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