Click here to Skip to main content
15,884,628 members
Articles / Programming Languages / C#
Article

Workaround Double Callback of FileSystemWatcher Event Handler

Rate me:
Please Sign up or sign in to vote.
3.50/5 (8 votes)
19 Jun 2006CPOL1 min read 86.6K   700   25   11
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.

C#
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)


Written By
Web Developer
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionA Solution that worked! Pin
Lakshya Sethi7-Jul-18 11:36
Lakshya Sethi7-Jul-18 11:36 
AnswerSystem.IO.FileSystemWatcher: Fix for Double Firing using EnableRaisingEvents in the Handler Pin
Garry Lowther17-Feb-09 1:01
Garry Lowther17-Feb-09 1:01 
QuestionWhere the deleted fileobject a directory or a file??? Pin
sympthom 913-Aug-06 4:03
sympthom 913-Aug-06 4:03 
GeneralPerhaps another workaround 2 Pin
nns nns26-Jul-06 14:55
nns nns26-Jul-06 14:55 
GeneralRe: Perhaps another workaround 2 Pin
Greg Roehm27-Jul-06 7:08
Greg Roehm27-Jul-06 7:08 
Generalhad this problem too Pin
Raduenzel27-Jun-06 4:02
Raduenzel27-Jun-06 4:02 
GeneralRe: had this problem too [modified] Pin
JMuFinn28-Jun-06 20:50
JMuFinn28-Jun-06 20:50 
GeneralRe: had this problem too Pin
Raduenzel29-Jun-06 2:07
Raduenzel29-Jun-06 2:07 
GeneralPerhaps another workaround... Pin
Andrew Rissing21-Jun-06 10:22
Andrew Rissing21-Jun-06 10:22 
QuestionWhat about when not deleting? Pin
Al_Pennyworth20-Jun-06 7:45
Al_Pennyworth20-Jun-06 7:45 
AnswerRe: What about when not deleting? Pin
DotNetEMT20-Jun-06 11:32
DotNetEMT20-Jun-06 11:32 

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

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