473,387 Members | 1,464 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

FileSystemWatcher Problem...

Hi,
I write an application which waits for incomming files in a specified
directory. I thought, that using the FileSystemWatcher would be the best, as
it does exactly what I need.
But now I have a problem:
I wonna get shure, that really all files are processed and I need to be
shure, that no file is processed twice.
If my application is started while there are already files in the directory
(which is realistic, as the application could be restarted, but the
application which produces the files may not be stopped at the same time). So
I thouhgt about just reading all the files which are already in the directory
and then starting the FileSystemWatcher for the new files which are arriving.
But what if a file arrives while I'm processing the old ones? So I thought
about first starting the FileSystemWatcher and then processing the old files.
But know I have the problem, that between starting the a file could be
processed twice (in a worst case cenario, where a new file arrives between
starting the FileSytemWatcher and reading the old files).

Maybe I'm just thinking to much, but is there any clean way to solve the
problem? Can I use the FileSystemWatcher instance for getting the old files
somehow?

The only solution I came up with, is to forget about the filesystemwatcher
and use a timer which checks for new files. But as there may be a longer time
period with no new files it would not be the best solution, because I would
have to let the timer look for new files about every 3 to 5 seconds, as some
of the files are time critical.

Maybe someone of you has a solution for my worst case thinking.

greetings

Florian
Nov 17 '05 #1
3 4573
Stampede wrote:
Hi,
I write an application which waits for incomming files in a specified

<snip>

Does the files have unique filenames ?

If they do, here's how I would do it:

1. Start FileSystemWatcher
2. Grab all files in directory and process them, add each to a list
3. Grab new files through FSW, check if they are already in the list

The list, if it grows too big, could be killed a short while after
starting the app as it would probably only be needed to solve the issue
about processing a file twice due to the time window from starting FSW
to grabbing the list of files.

If the files don't have duplicate names it means that they are
overwritten and that has a whole host of other problems.

--
Lasse Vågsæther Karlsen
http://www.vkarlsen.no/
mailto:la***@vkarlsen.no
PGP KeyID: 0x2A42A1C2
Nov 17 '05 #2
FSW can be difficult to get use to. Unfortunately your goal of not getting
duplicate files isn't possible. You are going to have to handle this
contingency. The FSW can raise multiple events for the same file so you are
going to have to deal with it anyway. For example if you create a new file
in notepad and then save it you'll get at least a create notification and a
change notification. If the file is complex enough to warrant multiple saves
then you'll get multiple change notifications. I work around this solution
by either keeping track of the files that I have already processed or by
simply reprocessing them anyway (unless it would cause problems). Take as an
example a file replication program that copies files from one folder to
another. If I should happen to copy a file twice it really won't matter that
much. The only downside is the wasted cycles but since I would be running on
an arbitrary thread anyway it wouldn't matter.

As for the startup order I would recommend starting FSW first and then back
process the files. That way you won't miss any files. Otherwise there is
always the chance that a file could come in between the time you get the list
of files and start the FSW.

Michael Taylor - 9/5/05

"Stampede" wrote:
Hi,
I write an application which waits for incomming files in a specified
directory. I thought, that using the FileSystemWatcher would be the best, as
it does exactly what I need.
But now I have a problem:
I wonna get shure, that really all files are processed and I need to be
shure, that no file is processed twice.
If my application is started while there are already files in the directory
(which is realistic, as the application could be restarted, but the
application which produces the files may not be stopped at the same time). So
I thouhgt about just reading all the files which are already in the directory
and then starting the FileSystemWatcher for the new files which are arriving.
But what if a file arrives while I'm processing the old ones? So I thought
about first starting the FileSystemWatcher and then processing the old files.
But know I have the problem, that between starting the a file could be
processed twice (in a worst case cenario, where a new file arrives between
starting the FileSytemWatcher and reading the old files).

Maybe I'm just thinking to much, but is there any clean way to solve the
problem? Can I use the FileSystemWatcher instance for getting the old files
somehow?

The only solution I came up with, is to forget about the filesystemwatcher
and use a timer which checks for new files. But as there may be a longer time
period with no new files it would not be the best solution, because I would
have to let the timer look for new files about every 3 to 5 seconds, as some
of the files are time critical.

Maybe someone of you has a solution for my worst case thinking.

greetings

Florian

Nov 17 '05 #3
There is another problem with FSW...if too many files are changed at the
same time (for ex, the moving of a directory containing 10000 files), the
FSW queue will be important. When it has not enough space (have a look to
MSDN to know the numbers), it can not store new events and they will be
lost...
To be sure to catch all events, you must start FSW BEFORE getting the first
list of files. Deal with events to manage your list but try to identify when
FSW has to many events to deal with. In that case, you have to update your
entire file list.

Hope it helps,

Ludovic Soeur.

"Stampede" <St******@discussions.microsoft.com> a écrit dans le message de
news:31**********************************@microsof t.com...
Hi,
I write an application which waits for incomming files in a specified
directory. I thought, that using the FileSystemWatcher would be the best, as it does exactly what I need.
But now I have a problem:
I wonna get shure, that really all files are processed and I need to be
shure, that no file is processed twice.
If my application is started while there are already files in the directory (which is realistic, as the application could be restarted, but the
application which produces the files may not be stopped at the same time). So I thouhgt about just reading all the files which are already in the directory and then starting the FileSystemWatcher for the new files which are arriving. But what if a file arrives while I'm processing the old ones? So I thought
about first starting the FileSystemWatcher and then processing the old files. But know I have the problem, that between starting the a file could be
processed twice (in a worst case cenario, where a new file arrives between
starting the FileSytemWatcher and reading the old files).

Maybe I'm just thinking to much, but is there any clean way to solve the
problem? Can I use the FileSystemWatcher instance for getting the old files somehow?

The only solution I came up with, is to forget about the filesystemwatcher
and use a timer which checks for new files. But as there may be a longer time period with no new files it would not be the best solution, because I would have to let the timer look for new files about every 3 to 5 seconds, as some of the files are time critical.

Maybe someone of you has a solution for my worst case thinking.

greetings

Florian

Nov 17 '05 #4

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

Similar topics

4
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...
0
by: cxw0106 | last post by:
Hello, I have some weird problem with the FileSystemWatcher. I have developed an application that monitors a network directory for file changes of a certain file type. The program runs well for...
2
by: Paul | last post by:
Hi, I've been developing an application in VB.NET that uses the FileSystemWatcher and a popup notification in order to tell me when files have been downloaded. The FileSystemWatcher code in...
7
by: Allen Anderson | last post by:
I'm trying to figure out a way to catch when a file has been written to a directory. I currently have it where I can catch when the file begins writing, but this isn't helpful as I need to know...
13
by: David | last post by:
I have been working on trying to write a directory watcher service. One of the requirments is that it be able to watch multiple directories, not sub directories of one parent directory, but just...
20
by: J-T | last post by:
We are working on an asp.net application which is a 3-tier application.I was aksed to create a component which monitors a folder and gets the file and pass them to a class library in our business...
1
by: PadovaBoy | last post by:
Hi! I try to develop a simple system for monitoring a sub directory of a web site and remake an xml file every time a sub-dir change it's name. I don't wont to use a window.service because, i want...
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...
5
by: =?Utf-8?B?Sm9obiBT?= | last post by:
I am trying to find out if there is a way to tell if there is already a filesystemwatcher (created by a webservice) monitoring a folder. I have a webservice that creates a filesystemwatcher,...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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...

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.