473,473 Members | 2,304 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Using FileSystemWatcher with large files

I'm setting up a watched folder using FileSystemWatcher.
When I drop a small file into the watched folder, I can
respond to a .Created event and process the file with
other code. However, if I try copying a large file into
the watched folder, the .Created event is fired off
immediately and not when the file is finished copying. I
have tried waiting for a certain number of .Changed
events to be fired after the initial .Created event, and
that method works greate except for the fact that some
files trigger 3 .Changed events, and others 5, 6, and 7.
I can't find a pattern to determine why there are a
differing number of events being fired for the various
files. Does anyone else have experience in this or have
any suggestions for determining when each file is
finished being written ? I'd rather not use a checking
loop or error handling for program flow, but I might have
to at this point.

Thanks in advance,
--JU
Jul 19 '05 #1
4 9054
Josh,

Whenever I use a FileSystemWatcher in this way I process the created files
in new threads. Each thread is added to a hashtable that is referenced by
the file name thus allowing me to see if i have already created a processor
for the file whe subsequent events are fired.

Within the processor thread, I open the file exclusively and continuously
loop until the open is sucessful.

Martin.

"Josh Usovsky" <jo**@usovsky.com> wrote in message
news:0b****************************@phx.gbl...
I'm setting up a watched folder using FileSystemWatcher.
When I drop a small file into the watched folder, I can
respond to a .Created event and process the file with
other code. However, if I try copying a large file into
the watched folder, the .Created event is fired off
immediately and not when the file is finished copying. I
have tried waiting for a certain number of .Changed
events to be fired after the initial .Created event, and
that method works greate except for the fact that some
files trigger 3 .Changed events, and others 5, 6, and 7.
I can't find a pattern to determine why there are a
differing number of events being fired for the various
files. Does anyone else have experience in this or have
any suggestions for determining when each file is
finished being written ? I'd rather not use a checking
loop or error handling for program flow, but I might have
to at this point.

Thanks in advance,
--JU

Jul 19 '05 #2
I've had similar problems using FileSystemWatcher to process large files
which are FTP'd to us each night. The event firing mechanism in
FileSystemWatcher is just flakey. While not ideal, using a polling loop is
the method that I use and it seems to be the only way around this problem.
When FileSystemWatcher fires the event, immediately try to open the file in
exclusive mode. If an exception is thrown, set the timer and then try to
open the file again when the timer fires. Once the file is available, stop
the timer and process the file.

A simpler method is to respond to the FileSystemWatcher event by
entering a loop which includes a Thread.Sleep() call after each failure to
open the file. The downside of this approach is that you're causing a
threadpool thread to block during the Sleep() call. Nevertheless, 95% of the
time the Sleep() loop is the way to go. It's just simpler.

David

"Josh Usovsky" <jo**@usovsky.com> wrote in message
news:0b****************************@phx.gbl...
I'm setting up a watched folder using FileSystemWatcher.
When I drop a small file into the watched folder, I can
respond to a .Created event and process the file with
other code. However, if I try copying a large file into
the watched folder, the .Created event is fired off
immediately and not when the file is finished copying. I
have tried waiting for a certain number of .Changed
events to be fired after the initial .Created event, and
that method works greate except for the fact that some
files trigger 3 .Changed events, and others 5, 6, and 7.
I can't find a pattern to determine why there are a
differing number of events being fired for the various
files. Does anyone else have experience in this or have
any suggestions for determining when each file is
finished being written ? I'd rather not use a checking
loop or error handling for program flow, but I might have
to at this point.

Thanks in advance,
--JU

Jul 19 '05 #3
"David Sworder" <ds******@cts.com> wrote in
news:eD**************@TK2MSFTNGP12.phx.gbl:
The event firing mechanism in FileSystemWatcher is just
flakey.


The event firing system is Windows, and it's not
guaranteed to be one event per file operation.

If you read the documentation, you'll find where it says
"Common file system operations might raise more than one
event" and "when a file is moved from one directory to
another, several OnChanged and some OnCreated and OnDeleted
events might be raised".

Mark
Jul 19 '05 #4
> If you read the documentation, you'll find where it says
"Common file system operations **might** raise more than one
event" and "when a file is moved from one directory to
another, **several** OnChanged and some OnCreated and OnDeleted
events **might** be raised".


It's words like "might" and "several" that bother me. I wish things were more precise. Not to say that FileSystemWatcher is useless by any means -- it's just not as consistent as I'd prefer.

David

Jul 19 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Troy Murphy | last post by:
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...
1
by: Megan | last post by:
I have a windows service that monitors a folder. I have a routine to process XML files whenever a new file gets created (in the folder that the Filesystemwatcher is monitoring.) Since the files...
0
by: Rich Wallace | last post by:
Hello all, Looking for suggestions and tips if possible. I have an application running on a file server that utilizes the FileSystemWatcher to trap when any Excel files are saved by a user. I...
2
by: kmcnet | last post by:
Hello Everyone and thanks for your help in advance. I have been battling a problem for nearly a month with the FileSystemWatcher component. Basically, what I am trying to do it to monitor three...
11
by: Josh Usovsky | last post by:
I'm setting up a watched folder using FileSystemWatcher. When I drop a small file into the watched folder, I can respond to a .Created event and process the file with other code. However, if I try...
2
by: RMB | last post by:
I am using the filesystemwatcher to track the movement of files from folder to folder. I am tracking how long it takes for each file to get from point A to point B. I have been able to do this,...
12
by: ljh | last post by:
Has anyone else noticed that the FileSystemWatcher raises the changed event twice when a file is changed? Do you have any idea why this is the case?
0
by: mabra | last post by:
Hi All ! I am fighting with watching a large amount of files for their content and look for some keywords, which indicate dangerous error situations to me. This costs me currently hours each...
7
by: =?Utf-8?B?Tmljaw==?= | last post by:
Hello, I've got a FileSystemWatcher object setup to check for new files in a folder. I've never done much with threading, but I think I mght need to here. When I find a new file there are times...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.