By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,570 Members | 1,223 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,570 IT Pros & Developers. It's quick & easy.

FileSystemWatcher Changed Event Double Activation

P: n/a
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
Feb 7 '08 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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

"tomb" <to**@technetcenter.comwrote in message
news:2z*******************@bignews7.bellsouth.net. ..
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

Feb 7 '08 #2

P: n/a
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

Lance Wynn wrote:
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

"tomb" <to**@technetcenter.comwrote in message
news:2z*******************@bignews7.bellsouth.net. ..
>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

Feb 7 '08 #3

P: n/a
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.
"tomb" <to**@technetcenter.comwrote in message
news:2X*******************@bignews7.bellsouth.net. ..
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

Lance Wynn wrote:
>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

"tomb" <to**@technetcenter.comwrote in message
news:2z*******************@bignews7.bellsouth.net ...
>>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
Feb 7 '08 #4

P: n/a
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
Lance Wynn wrote:
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.
"tomb" <to**@technetcenter.comwrote in message
news:2X*******************@bignews7.bellsouth.net. ..
>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

Lance Wynn wrote:
>>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

"tomb" <to**@technetcenter.comwrote in message
news:2z*******************@bignews7.bellsouth.ne t...
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
Feb 7 '08 #5

This discussion thread is closed

Replies have been disabled for this discussion.