FileSystemWatcher and multiple, fast changes

S

Steve

I have a FileSystemWatcher watching a directory where I will add and update
files. When it detects a change, I search through all the files and update
my list of files in the UI. This works fine.

Now, my other application that is writing the files that are being watched,
does something like this:
1) Creates a file, a.txt
2) wants to name it b.txt but checks first if b.txt exists, if it does, it
delete it
3) moves a.txt to b.txt (rename)

what happens is that when b.txt is deleted, my other app that is watching
that folder starts processing the files in the directory(Creating FileInfo
objects, etc) In that processing, it's looping through a collection of
files that WERE there when it was initiated.

Then the call to Move() happens and now the FileSystemWatcher app has
invalid data as it hasn't returned fast enough to finish before the move()
call happened.

I hope this makes sense to someone. Basically, application A is deleting
resources that Application B is still working with.

Do I just catch the FileNotFoundException and deal with it? Any design
suggestions to work with this scenario?
 
S

Steve

Steve said:
I have a FileSystemWatcher watching a directory where I will add and update
files. When it detects a change, I search through all the files and update
my list of files in the UI. This works fine.

Now, my other application that is writing the files that are being
watched, does something like this:
1) Creates a file, a.txt
2) wants to name it b.txt but checks first if b.txt exists, if it does,
it delete it
3) moves a.txt to b.txt (rename)

what happens is that when b.txt is deleted, my other app that is watching
that folder starts processing the files in the directory(Creating FileInfo
objects, etc) In that processing, it's looping through a collection of
files that WERE there when it was initiated.

Then the call to Move() happens and now the FileSystemWatcher app has
invalid data as it hasn't returned fast enough to finish before the move()
call happened.

I hope this makes sense to someone. Basically, application A is deleting
resources that Application B is still working with.

Do I just catch the FileNotFoundException and deal with it? Any design
suggestions to work with this scenario?

This seems like I hack, but I added a bool flag to determine if I'm working
with files or not, something like this:
<code>
private void OnWatchedDirectoryContentsChanged(object source,
FileSystemEventArgs e)
{
if (_workingWithA43Files == false)
{
LoadAvailableFiles();
}
}
</code>

Inside LoadAvailableFiles() I set _workingWithA43Files true at the start
and false when I'm done.
This isn't working consistently though...
 
F

Frank Rizzo

Steve,

Back in the VB2 days (long before FileSystemWatcher) I used a semaphore
approach. Basically both applications knew that SomeFile.sem was the
semaphore file. The procedure to do work on a certain file was like this:

1. Does SomeFile.sem exist? If yes, the other application is doing
work. Try again later. If file doesn't exist, go to step 2.
2. Create SomeFile.sem. On success go to step 3. On failure, start over.
3. Do work.
4. Delete the semaphore file.

This method worked flawlessly. Never had any problems with it.
These days, I would use a system semaphore via P/Invoke (unless .net2.0
has something I don't know about), but back then I didn't know how to
access a semaphore from vb2.

As far as your current situation, handling FileNotFoundException is a
good first step, but given that both applications access resources in a
non-managed way, you are basically asking for trouble and working
without a net.


Regards
 

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