FileSystemWatcher HELP!!!

G

Guest

Hi! I keep running into really odd behaviour with this object and I've been
working with it for over 2 moths now. I really need someone's help who has
delt with it before. I'll list below the logic of what my application does.
It would be even better if someone would be willing to review my code which
for reasons of being pretty extensive will not be included here. In short,
the FSW is not working consistently in our production environment while
watching only a handfull of files. Any help is greatly appreciated, thanks
in advance!

1) Service runs every 30 minutes
2) checks database for any records for files to watch
3) locates those records, creates 1 FSW object for each record with
File Name and Path pointing to the info in database, wires up OnChanged,
OnRenamed, OnCreated, and OnDeleted events to their handlers
4) Adds the instance of the FSW object to global shared Hashtable
5) Updates SQL status for this file to 2, meaning that FSW created, waiting
for the file to be generated/updated.
6) OnChanged event contains the code that reacts on 2 types of events
OnChanged and OnCreated. When it fires, it checks that the instance of FSW
that raised the event is contained in global hashtable. If so, it connects to
database and gets the XML options for this specific file (such as FTP file,
email it, back it up, etc...)
7) In OnChanged event there's loop inside of which it keeps trying to rename
the file that has raised the event to a new name (just appends one to the
end of the file name). This is done so that if the file is very large it
would not
try to work with it (FTP or something else) until it's fully written.
Once it's able to rename it, it means the system no longer has a lock on the
file. It then renames the file to its original name and kicks off the
function that process the XML actions associated with this file.
8) Upon completion of all the actions or in case of failure the SQL status
for this file is updated (1 for success, 3 for failure.
9) The reference for this FSW object is removed from global hashtable.
10) Same thing is done for any other files that have been picked up by the
service
when it ran initially with same logic.
11) Service runs once again in 30 minutes.
 
M

Michael Groeger

Hi Alex,

can please explain step 7 more detailed? I don't understand why you are
trying to rename the file. And what is the exact problem which happens?

Michael
 
G

Guest

Thanks for taking an interest in this topic! I think I have actually
explained the reason for renaming. Sometimes the files that I'm watching
(FSW object watching) are generated by some reporting application and those
files could be 20-40 megs in size. While they are being written to the
folder/filename that's being monitored they are locked by the system, thus
trying to say FTP them somewhere would fail, also even if it would not fail
it would only send a partial file since it's still being written. This loop
ensures that I perform the actions specified in the XML configuration only
when the file completely done and is accessible to any applications.

As to what problems I run into, here are a couple. One, sometimes (and in
no particual order) the files are not picked up by the FSW at all even though
I see in the database and the logs (I write extensive logs on all actions in
the code) that the FSW object is created for these files and supposedly it's
watching dir/filename, but when the file generated, the OnChanged/OnCreated
event does not fire at all. Thus the file is not processed and it just sits
there and still waits for it. If i am to copy another copy of the same file
with the name that FSW object expects later on, it fires just fine. The only
difference at that point is that I copy it using Windows Explorer, as oppose
to it being generated by some application.
Another problem I run into earlier when I used the OnRenamed event to watch
for that renaming procedure I now do in the same OnChanged event is that
sometimes the onChanged event would fire, I'd rename the file, but the
OnRenamed event would not fire after that even though the renaming was
explicitely done. So I'd just have a file with appended 1 on the end sit in
that folder and not being processed any further. That's why I moved the
whole logic of renaming and kicking off the xml processing function back to
OnChanged event.

Hope this will help you with suggestions for my problems. Thanks!
 

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