Click here to Skip to main content
Licence CPOL
First Posted 19 Jun 2006
Views 41,399
Downloads 251
Bookmarked 22 times

Workaround Double Callback of FileSystemWatcher Event Handler

By | 19 Jun 2006 | Article
If you need to process a file as soon as it is created, the FileSystemWatcher is your solution, but it can confound you.
Sample Image - MyExampleOutput.png

Introduction

If you've tried to use the FileSystemWatcher to wake up and process a newly created file in some directory, you have likely experienced what I did after coding the simple example found in this Microsoft Tutorial. When testing in either Debug or Release mode (outside the debugger), the Event Handler is invoked twice for a single file dropped in the target directory.

For my particular application, this wouldn't be fatal since I will need to scan the entire directory at "process" time and once the files are processed they are deleted. In other words, a second call to a method that reads in a list of existing filenames and then dispatches a worker method to load a database with the data contained in those files is an innocuous operation.

Nonetheless, in my testing, I was confounded as to why the double callbacks occurred. After searching the Web, I found little in the way of a fix. I did find several other folks that found the double callback behavior problematic for a variety of reasons.

The workaround is premised on my best guess that the Framework is notified by the OS when the file handle is created/opened and then again when it is closed (file writing is completed). Given that, I added a FileInfo inquiry to the event handler which asks the OS if the file which has been reported as CREATED is, in fact, in the file system. Seems that on the first callback, this test consistently fails.

The highlights from my C# proof-of-concept project are below with the entire project in the zip download.

static void Main(string[] args)
{
    watcher = new FileSystemWatcher();
    watcher.Path = mailbox;
    watcher.NotifyFilter = NotifyFilters.FileName;
    watcher.Created += new FileSystemEventHandler(OnChanged);
    watcher.EnableRaisingEvents = true;

    Console.WriteLine("Press Enter to quit\r\n");
    Console.ReadLine(); 
}

public static void OnChanged(object source, FileSystemEventArgs e)
{
    watcher.EnableRaisingEvents = false;
    FileInfo objFileInfo = new FileInfo(e.FullPath);
    if (!objFileInfo.Exists) return; // ignore the file open
    ProcessAllFiles(e.FullPath);
}

History

  • 20th June, 2006: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

DotNetEMT

Web Developer

United States United States

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
AnswerSystem.IO.FileSystemWatcher: Fix for Double Firing using EnableRaisingEvents in the Handler PinmemberGarry Lowther1:01 17 Feb '09  
QuestionWhere the deleted fileobject a directory or a file??? Pinmembersympthom 94:03 13 Aug '06  
GeneralPerhaps another workaround 2 Pinmembernns nns14:55 26 Jul '06  
I have seem similar situations being handled in a different manner.
 
Lets say you have a folder where files are being dropped from a remote location at regular intervals. You would like to be notified of new files being available, to do further processing on them.
 
In this scenario, instead of using the NotifyFilters.FileName property, use the NotifyFilters.LastWrite Property to trigger the event handler. This will prevent a double callback and make sure that the file is completely transfered before triggering the change event.
 
Thanks,
-NS.
GeneralRe: Perhaps another workaround 2 PinmemberGreg Roehm7:08 27 Jul '06  
Generalhad this problem too PinmemberRaduenzel4:02 27 Jun '06  
GeneralRe: had this problem too [modified] PinmemberJMuFinn20:50 28 Jun '06  
GeneralRe: had this problem too PinmemberRaduenzel2:07 29 Jun '06  
GeneralPerhaps another workaround... PinmemberAndrew Rissing10:22 21 Jun '06  
QuestionWhat about when not deleting? PinmemberAl_Pennyworth7:45 20 Jun '06  
AnswerRe: What about when not deleting? PinmemberDotNetEMT11:32 20 Jun '06  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web02 | 2.5.120529.1 | Last Updated 20 Jun 2006
Article Copyright 2006 by DotNetEMT
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid