FileSystemWatcher Changed Event Double Activation

T

tomb

I'm using VB.net 2003 and a FileSystemWatcher. The Changed event gets
called twice for every change in the folder I'm watching. To build the
functionality, I'm manually changing a text file. As soon as I save the
changed file, the event is called, which is great. But then the event
is called again almost immediately. That causes some serious issues for
my program.

Has anyone seen this before? Is there a way to stop it?

Thanks

Tom
 
L

Lance Wynn

I had a problem with this when my notifyfilter contained 2 filters that
could both conveivably be hit with a single change ie:

fsw.NotifyFilter = NotifyFilters.LastWrite Or NotifyFilters.Size

If I write to the file, and the filesize also changes, then this gets raised
once for the lastwrite, and once for the size change. (In the case of
notepad, it seems the Size change happens regardless as I think notepad
re-writes the complete file.

In my case, I just used the lastwrite notification filter, and that solved
my problem, because all I cared about was whether the file had been written
to.



Lance
 
T

tomb

This sounded like it should resolve the issue, but it didn't. I set the
filter to lastwrite, and the changed event still gets called twice. Is
this a bug in V2003?

T
 
L

Lance Wynn

It must be the way you are testing the file. If you are using notepad, I
think it resets the file, and rewrites the whole thing.
In my case, I had a lazy reader that read the tail end of a log file when it
changed. As I recall I saw this problem when using notepad to add lines to
the end of the file. I just tested with both filters in place, and used:
echo test >> test.txt
from a command line to append text to the end of the file, and it works as
expected even with both filters in place. So I suspect my original post was
incorrect as to the cause. And the actual culprit is the way notepad
modifies the file.
 
T

tomb

Well, I tested it using the program that would actually be affecting the
files. It is a vb.net application using a dataset. The dataset just
calls WriteXml() to save the data it is holding.

My FileSystemWatcher captures the file update, but it is still being
activated twice. This is not good.

Can you think of some way around this? I was thinking of using a flag,
but I can't figure out how to make that work. The file could
conceivably be updated every 60 seconds. I could deactivate the watcher
for 20 seconds immediately upon activation, but that seems so cloogy.

Thanks,

T
 
T

tomb

I looked at that page - it really didn't offer anything new. But, I did
figure out how to accommodate this "feature" that you may want to make
note of. I have a static date variable, timeSet, that initially equals
12:00 am. Another variable, nTime, captures Now. If timeSet >
nTime.AddSeconds(-30) then return.

For my program, anything less than 30 seconds means the event was raised
a second time. All my file updates happen in 60 second increments. Of
course, the file name must be compared also.

Thanks for your help.

T
 

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