Robust solution for processing files in a folder

G

Guest

Hello,

I am writing an application that processes xml files that are stored in a
folder. The application picks up the files in a folder, processes them and
then deletes them.

What is the best approach on loading the xml files from the folder into my
application?

At the moment I am using the System.IO.FileSystemWatcher to monitor files
that are created in the folder (created is same as copied to) and then I add
the filename to a Queue object. This has as drawback that if files are in the
folder before the application is started I have to load the into my
application manually.

If I have to write code to load the files that are already in folder before
the FileSystemWatcher becomes active, why would I even use the
FileSystemWatcher? I could use a timer and scan the directory on every
Timer.Elapsed.

I need a robust solution for 'monitoring' a folder and processing the files
in a Windows Forms application.

Philip Wagenaar
 
G

Guest

I need a robust solution for 'monitoring' a folder and processing the files
in a Windows Forms application.

I would use both FileSystemWatcher and a Timer. I would arrange for the
FileSystemWatcher to enable the Timer with interval=1 (ie an immediate tick)
whenever it sees the kind of change you want. I would arrange for the Timer
tick code to process all files it finds in the folder. If the Timer tick
code finds nothing in the folder, increase the interval up to a maximum,
something like:
interval *= 10
if interval >10000 then interval = 10000 ' 10 sec max
If the Timer tick code finds at least one file in the folder, decrease the
interval:
interval = 1

This design achieves the following. When no files are arriving, you will be
lightweight. When no files are arriving, you will periodically take a peek
at the folder anyway, and thus you are robust in the event of a
FileSystemWatcher failure. When files are arriving, you will get them all
without fail every time. FileSystemWatcher use is as simple as it can be, ie
when event tweak a timer, and nothing else.

In case you haven't noticed, I have some mistrust of FileSystemWatcher. I
believe its underlying technology is api ReadDirectoryChangesW, and some
years ago I had some difficulties with its reliability. Today, perhaps my
mistrust is unwarranted, but old attitudes are hard to change.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top