FileSystemWatcher: problems with event-catching

  • Thread starter Thread starter Andreas
  • Start date Start date
A

Andreas

Hi,

we have some problems with the following situation:
A remote application A writes files to a directory monitored by a
FileSystemWatcher-instance in application B. A creates the file
with the initial ending "COPY_IN_PROGRESS" and only after
successful copying the file gets renamed to its final ending
"TO_DO". The FileSystemWatcher of B is set to react on the
rename-event and upon recognizing an event some processing
is triggered.
Now the problem is that obviously the event is raised and also
recognized, however the renamed file seems to not yet exist
within the filesystem: the given RenamedEventArgs contain the
new filename, however a File.Exists on this filename, executed
first within the OnEvent-method, returns with false.
Though, if some trace messages are inserted before the File.Exists
everything is working fine, so it seems that there might be some
sort of timing problem. Is it possible that the rename event is
raised before completion of the file system update.
Does anyone know something about this problem or has a solution
ready? (inserting some delay wouldn't be a proper solution; a
completely event-driven solution is preferred)

Thanks in advance
Andreas
 
The Rename and create events of the filesystemwatcher both are quite
eager -- that is, you get the events early, and may not be able to yet
access the file in question.

For any application / service I have written using the FSW, I've normally
done the following
1) Upon getting the event, spawn a "processing thread" and start it up
2) in the processing thread, try to access the file repeatedly until
sucessful (sleeping a bit in-between tries)
3) Let the processing thread completely process the file, the notify the
application thread that the contents are ready.
 
Back
Top