FileSystemWatcher problem - multiple hits

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance

From: zfeld (zfeld100_at_hotmail.com)
Date: 01/05/05


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



Relevant Pages

  • Re: FileSystemWatcher problem - multiple hits
    ... > I am serializing an object to XML and writing it to disk. ... > twice.This is causing me to process the change twice (causing a ... What are you using to edit the XML file? ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: What tool to use for processing large documents
    ... cannot parse faster than the disk can read the XML data. ... Reading 10 GB off a disk will take around 3 to 5 minutes ... I forgot to mention that my logs are in zipped xml. ... Get the set of nodes matching an XPath expression. ...
    (comp.text.xml)
  • Re: Creating a recovery disk that will work on SATA drives
    ... I have not personally heard of Drive Image XML. ... images as i have stated but was unable to even start the bsrtpe disk that was created with Win xp pro with sp1 as i was getting a bsod basically saying it could not find my drive, so u downloaded the driver pack and intergrated it into bart pe and was able to start bart pe and access driveimage xml to start the restore process. ... I am trying to create a recovery disk set that will allow me to image and restore my hard drive, however my last attempt using BARTPE and Driveimaxe xml failed. ...
    (microsoft.public.windows.vista.general)
  • Re: Creating a recovery disk that will work on SATA drives
    ... I am able to create images as i have stated but was unable to even start the bsrtpe disk that was created with Win xp pro with sp1 as i was getting a bsod basically saying it could not find my drive, so u downloaded the driver pack and intergrated it into bart pe and was able to start bart pe and access driveimage xml to start the restore process. ... I am trying to create a recovery disk set that will allow me to image and restore my hard drive, however my last attempt using BARTPE and Driveimaxe xml failed. ...
    (microsoft.public.windows.vista.general)
  • Re: Is it possible for rubyzip to return zipped data instead of writing it to the disk?
    ... | I got simple Rails app and I'd like to compress xml data and send it ... I'm trying to use rubyzip for it, ... | returns nil and creates file on the disk. ... cache generated XML datta to file). ...
    (comp.lang.ruby)