FileSystemWatcher is Stable

T

Troy Murphy

How do I prevent the FileSystemWatcher event to keep firing while the file
is being created? When copying a file to the watched folder, the event
fires a dozen or more times! Also, the documentation states that if more
than one file is copied to the folder at the same time, a cache will queue
them up for processing, but when I drop 3 files into this folder, only the
1st (sometimes the first two) cause the event to fire.

Thanks,

Troy



Here is the problamatic Code Fragment:

protected void ListenerMethod()

{

try

{

// Setup the Watcher

FileSystemWatcher Watcher = new FileSystemWatcher();

Watcher.Path=@"C:\WatchedFolder";

Watcher.Filter="*.bbf";

Watcher.NotifyFilter=System.IO.NotifyFilters.LastAccess |
System.IO.NotifyFilters.LastWrite;

// Add event handlers.

Watcher.Changed += new FileSystemEventHandler(OnChanged);

Watcher.Created += new FileSystemEventHandler(OnChanged);

Watcher.Deleted += new FileSystemEventHandler(OnChanged);

Watcher.Renamed += new RenamedEventHandler(OnRename);

// Begin watching file events

this.Log("Starting File Watcher service for '"+Watcher.Filter+"' on folder:
"+Watcher.Path.ToString());

Watcher.EnableRaisingEvents=true;

}

catch(Exception ex)

{

this.Log("Could not Start the Listener.
"+ex.Message,System.Diagnostics.EventLogEntryType.Error);

}

}

protected void OnRename(object source, RenamedEventArgs e)

{

this.Log("File Renamed from: "+e.OldName.ToString()+" to:
"+e.Name.ToString());

this.OnChanged(source, (FileSystemEventArgs)e);

}

protected void OnChanged(object source, FileSystemEventArgs e)

{

//Handles the event fired by the watcher

string FileName = e.FullPath;

System.IO.FileInfo oFile = new FileInfo(FileName);

string Action = e.ChangeType.ToString();

this.Log("Received FileWatcher "+Action+" event for file: "+FileName+"
("+oFile.Length.ToString()+" bytes)");

if (Action=="Changed")

{

try

{

this.Log("Sending file: "+FileName+" to Fox BackupHandler dll");

if (this.Process(FileName))

this.Log("Backup Event Handler reported Success");

else

this.Log("Backup Event Handler Reported: "+this.LASTSTATUS.ToString()+".
Last Error =
"+this.LASTERROR.ToString(),System.Diagnostics.EventLogEntryType.Warning);

}

catch(Exception ex)

{

this.Log(ex.Message+". Backup Event Handler Reported:
"+this.LASTSTATUS.ToString()+". Last Error =
"+thisl.LASTERROR.ToString(),System.Diagnostics.EventLogEntryType.Error);

}

}

}
 
D

David Sworder

Hi Troy,

I know this isn't the answer that you want, but the FileSystemWatcher is
just flakey. Some time ago, I posted a message lamenting the inconsistencies
of the FileSystemWatcher. (Someone correctly pointed out that my anger
shouldn't be directed at the .NET FileSystemWatcher, but rather the O/S
which fires file notifications in an inconsistent manner.) The bottom line
is that you can only use the FileSystemWatcher as a rough guide for file
system activity. When the FSW event fires, immediately stop FSW
notifications and see if the files you want to process are available for
exclusive access. If they aren't, set a timer and try again, etc.. Once
you've processed all of your files, kill the timer and reactivate the FSW
notifications.

Sorry I don't have better news. :(

David
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top