D
David Jackson
Hello,
I have a Windows service written in C# which uses FileSystemWatcher to
monitor a network folder for the creation of certain files. These are text
files which originate from a remote mainframe system.
When (a) file(s) arrive in the network folder, the Created event fires which
then kicks of another method within the Windows service which processes the
file.
However, the files might be several megabytes in length, and the problem I'm
having is that the FileSystemWatcher event fires as soon as the file
creation process begins, not when it ends, so for larger files this can fail
because the file itself is still being written when the Windows service
tries to start processing it.
I understand that this is by design.
Having done a bit of a trawl through Google, I found this article:
http://www.thescripts.com/forum/thread447494.html which suggests (among
other things), to set up a loop with a sleep timer to keep trying to rename
the file, the idea being that as soon as it is possible to rename the file
that must mean that the creation process has finished. Makes sense.
I also found this Microsoft article:
http://www.thescripts.com/forum/thread447494.html which suggests just
putting the thread to sleep for 500ms anyway, thereby giving the file
creation process enough time to complete. I could easily set that to
something huge like 30 seconds, I suppose, but that doesn't seem
particularly robust or efficient.
I'd be interested how other people have solved this issue.
Thank you.
DJ
I have a Windows service written in C# which uses FileSystemWatcher to
monitor a network folder for the creation of certain files. These are text
files which originate from a remote mainframe system.
When (a) file(s) arrive in the network folder, the Created event fires which
then kicks of another method within the Windows service which processes the
file.
However, the files might be several megabytes in length, and the problem I'm
having is that the FileSystemWatcher event fires as soon as the file
creation process begins, not when it ends, so for larger files this can fail
because the file itself is still being written when the Windows service
tries to start processing it.
I understand that this is by design.
Having done a bit of a trawl through Google, I found this article:
http://www.thescripts.com/forum/thread447494.html which suggests (among
other things), to set up a loop with a sleep timer to keep trying to rename
the file, the idea being that as soon as it is possible to rename the file
that must mean that the creation process has finished. Makes sense.
I also found this Microsoft article:
http://www.thescripts.com/forum/thread447494.html which suggests just
putting the thread to sleep for 500ms anyway, thereby giving the file
creation process enough time to complete. I could easily set that to
something huge like 30 seconds, I suppose, but that doesn't seem
particularly robust or efficient.
I'd be interested how other people have solved this issue.
Thank you.
DJ