FileSystemWatcher problem - multiple hits
From: zfeld (zfeld100_at_hotmail.com)
Date: 01/05/05
- Next message: Sahil Malik: "Re: Da.fill(ds); --error"
- Previous message: James Curran: "Re: if comparison: value first"
- Next in thread: Sahil Malik: "Re: FileSystemWatcher problem - multiple hits"
- Reply: Sahil Malik: "Re: FileSystemWatcher problem - multiple hits"
- Reply: Richard Grimes [MVP]: "Re: FileSystemWatcher problem - multiple hits"
- Messages sorted by: [ date ] [ thread ]
Date: Wed, 5 Jan 2005 17:09:38 -0500
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
- Next message: Sahil Malik: "Re: Da.fill(ds); --error"
- Previous message: James Curran: "Re: if comparison: value first"
- Next in thread: Sahil Malik: "Re: FileSystemWatcher problem - multiple hits"
- Reply: Sahil Malik: "Re: FileSystemWatcher problem - multiple hits"
- Reply: Richard Grimes [MVP]: "Re: FileSystemWatcher problem - multiple hits"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|