Two tips I'm using, maybe they can help.
First one:
If you can control the sending application, it should first write the file
and then rename it.
Your app could listen then only rename changes. This way the file will be
readable when
you get the change event.
Like: sending app sends FILE.XXX and then renames it as FILE.TXT.
You intercept the rename and read it.
Second:
Use a semaphore file to indicate end of transmission. When you find create
event for the semaphore file,
scan the directory. Requires that you can control the sending app, of
course.
"Stefan L" <slueck@pp-software.REMOVEIfNoSpam.deha scritto nel messaggio
news:uDL8wCjtGHA.1288@TK2MSFTNGP02.phx.gbl...
Quote:
Hi NG,
>
I have a file driven application (a report server) which has to do some
work when new files arrive or are deleted.
>
When processing the notifications about newly created files from a
FileSystemWatcher I want to open the file and read some info from it.
Problem is that when opening the file directly when the event is fired I
usually get a "file used by another process".
>
Has anyone figured out a good way how to handle such events? Is there an
option to get a notification when the file is created and can be read?
>
My other alternatives I thought about, were:
1. Use a FIFO in a second thread that processes the files which are new.
Should keep iterating (with a short Thread.sleep) until all files are
imported successfully.
2. Start a worker thread from the pool for each file which tries to read
it in.
3. Forget about FileSystemWatcher and check the directory for new files
every 30 secs (Timer driven) [but how to reco if files have changed
here?].
>
Does anyone has experience with this? Any best practices?
>
TIA,
Stefan