file system watcher question

  • Thread starter Thread starter ToddT
  • Start date Start date
T

ToddT

i've got one app that writes large files to a specific directory that
is watched by another app via an instance of the file system watcher
class. my problem is that the second app is notified when files are
created, but the first app hasn't finished writing to it - causing a
"file is already open" error when the second app trys to open the file
for processing. what i need is a "file closed" event to be thrown by
the file system watcher object. any idea how i can get around this
problem? thx.
 
What you could to do is contain the monitor with in a while loop that will
exit when processed once.

Inside this loop have a try catch statement that catches the exception that
is thrown when a locked file is accessed. At this point you could then wait
for a certain amount of time, then try to access the file again, and if this
files, wait again etc... until you have access to the file.

so it may look something like this in the creation event handler:



int secondsToSleep = 5;
bool fileAccessed = false;

while ( !fileAccessed )
{
try
{
//
// try to read the file contents here
//

// successful read
fileAccessed = true;
}
catch ( UnauthorizedAccessException ex ) // is this the exception
thrown?
{
// could not access the file
System.Threading.Thread.Sleep ( secondsToSleep * 1000 );
}

}
 
thx for the reply.

yeah, that is pretty much what i am doing. it just feels like a hack.
i wish there was a "close file" event thrown by the fsw...
 
That would be nice. I suppose it's because the purpose behind the file
system watcher is for logging files that have been moved/rename/created, and
not for analysing the file details.
 
I came up with the same work around. I too feel like it's a hack. I
eventually dropped the system watcher for my own home grown version. I also
found it a little buggy on Windows Server 2000.

Dax
 
Yep, I don't used the file system watcher either. I reverted to a clunky
polling system. Hey, it works. ;o)
 
Back
Top