Thursday, October 21, 2004 11:23 PM bart

FileSystemWatcher event is triggered twice (solution)

I've seen this behavior during a demo of mine today using notepad and a filesystemwatcher that monitors a single file. According to the Visual Studio .NET documentation (but you have to search a bit to find it): "This behavior is by design". The funny fact is that Notepad is in fact playing the evil role in this scenario. Let me paste this info here:

Multiple Created Events Generated for a Single Action

You may notice in certain situations that a single creation event generates multiple Created events that are handled by your component. For example, if you use a FileSystemWatcher component to monitor the creation of new files in a directory, and then test it by using Notepad to create a file, you may see two Created events generated even though only a single file was created. This is because Notepad performs multiple file system actions during the writing process. Notepad writes to the disk in batches that create the content of the file and then the file attributes. Other applications may perform in the same manner. Because FileSystemWatcher monitors the operating system activities, all events that these applications fire will be picked up.

Note   Notepad may also cause other interesting event generations. For example, if you use the ChangeEventFilter to specify that you want to watch only for attribute changes, and then you write to a file in the directory you are watching using Notepad, you will raise an event . This is because Notepad updates the Archived attribute for the file during this operation.

For more information (if you have VS.NET installed with MSDN): ms-help://MS.VSCC.2003/MS.MSDNQTR.2003FEB.1033/vbcon/html/vbtbsTroubleshootingUNCPathNamesNotAcceptedOnNT4Machines.htm

Del.icio.us | Digg It | Technorati | Blinklist | Furl | reddit | DotNetKicks

Comments

# re: FileSystemWatcher event is triggered twice (solution)

Monday, October 25, 2004 4:33 PM by bart

The FileSystemWatcher also isn't that accurate: if you create a lot of files in a short time, the filesystemwatcher will skip some (I believe because of a in-memory buffer which is too small).

# re: FileSystemWatcher event is triggered twice (solution)

Monday, October 25, 2004 5:54 PM by bart

Hi Geert,

Thanks for your feedback. The remark about the accuracy of the FileSystemWatcher is true indeed. This has to do with the underlying Win32 API (*ChangeNotification functions; ReadDirectoryChangesW) being called by the framework and the use of an internal buffer with a default size of 4K (which can hold about 70-80 changes). Each event takes a series of bytes (16 to start with and additional ones for the Unicode-formatted filename). It's possible to modify the buffer size (with caution!) using the InternalBufferSize property. Since the contents of the buffer are non-paged in memory, you should avoid doing this as much as possible (swapping to disk will be denied) since this can imply a performance drop.

# re: FileSystemWatcher event is triggered twice (solution)

Thursday, November 04, 2004 11:25 PM by bart

Just use safe coding practices and do not (exclusively) rely on the returned file name. For example: for files that are created, check all file names to find the new files. Maybe less performant, but very lickely to be more accurate.

# re: FileSystemWatcher event is triggered twice (solution)

Friday, March 04, 2005 6:36 PM by bart

Hello,

I am trying to debug a problem with FileSystemWatcher. I have two methods in my application that monitor a folder (on different server) for any new files being created. When it finds it, it takes those .doc files and creates PDF files. There is another process that generates these doc files and puts it into this folder. Usually the files are somewhere beween 80 to 1500. I noticed that when the process drops these files into the folder, my application executes Renamed event and not Created events. Also, lot of the times it will stop in the middle with files still left in the folder. Sometimes it will process 80 out of 100 or 140 out of 145 etc. The numbers are always different.

So the two issues are:
1). Why does it execute renamed method and not created method.
2). Why does it stop with files still left in the folder?

I have set internal buffer to 16K since i can get couple of hundreds of files at once. Please advise. I been trying to solve this problem for few months now without any success. Below are the two methods...

Private WithEvents objWatcher As New System.IO.FileSystemWatcher()
objWatcher.NotifyFilter = IO.NotifyFilters.FileName
objWatcher.IncludeSubdirectories = False
objWatcher.InternalBufferSize = 16384
objWatcher.EnableRaisingEvents = True


Private Sub objWatcher_Renamed(ByVal sender As Object, ByVal e As System.IO.RenamedEventArgs) Handles objWatcher.Renamed
'Do Stuff
End sub

Private Sub objWatcher_Created(ByVal sender As Object, ByVal e As System.IO.FileSystemEventArgs) Handles objWatcher.Created
'DO Stuff
End Sub

# re: FileSystemWatcher event is triggered twice (solution)

Monday, April 18, 2005 3:55 AM by bart

????????????

# re: FileSystemWatcher event is triggered twice (solution)

Thursday, May 19, 2005 9:18 PM by bart

Notepad apparently causes the events to occure differently when you click "File-Save" (raises a single fsw created event) as compared to clicking "File-Save As" (raises a double fsw created event).

# Really COOL!

Monday, June 06, 2005 12:19 AM by bart

Wow, superb!
Thanks.

# re: FileSystemWatcher event is triggered twice (solution)

Monday, July 25, 2005 6:07 PM by bart

The filesytemwatcher surprised me. When you paste many files in the wathed directory, it skips some and work on others. i hope mycrosoft can expalin this. Any help ?

# FileSystemWatcher ed eventi generati pi

Sunday, April 01, 2007 3:16 AM by Around and About .NET World

# Multiple Events Fired for File System Watcher Event « {Programming} & Life

Pingback from  Multiple Events Fired for File System Watcher Event « {Programming} & Life

# How to Use FileSystemWatcher Instead of Polling

Sunday, December 30, 2012 9:23 AM by Fascinated with Software

How to Use FileSystemWatcher Instead of Polling

# rimonabantexcellence site title

Sunday, July 21, 2013 9:01 AM by rimonabantexcellence site title

Pingback from  rimonabantexcellence site title

# How to Use FileSystemWatcher Instead of Polling

Monday, August 11, 2014 6:13 AM by Fascinated with Software

How to Use FileSystemWatcher Instead of Polling

# How to Use FileSystemWatcher Instead of Polling

Monday, August 11, 2014 6:13 AM by Fascinated with Software

How to Use FileSystemWatcher Instead of Polling