Re: FileSystemWatcher Changed Event Double Activation

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



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


Lance Wynn wrote:
Here's a small bit of info I found through google,
http://weblogs.asp.net/ashben/archive/2003/10/14/31773.aspx

There may be some solutions in there that can help. I haven't tried any yet, but it seems this is a design feature.


"tomb" <tomb@xxxxxxxxxxxxxxxxx> wrote in message news:hsLqj.68636$Mu4.63021@xxxxxxxxxxxxxxxxxxxxxxxxx
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" <tomb@xxxxxxxxxxxxxxxxx> wrote in message news:2XHqj.68525$Mu4.63364@xxxxxxxxxxxxxxxxxxxxxxxxx
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" <tomb@xxxxxxxxxxxxxxxxx> wrote in message news:2zFqj.68460$Mu4.47698@xxxxxxxxxxxxxxxxxxxxxxxxx
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


.



Relevant Pages

  • Re: FileSystemWatcher Changed Event Double Activation
    ... My FileSystemWatcher captures the file update, but it is still being activated twice. ... As I recall I saw this problem when using notepad to add lines to the end of the file. ... from a command line to append text to the end of the file, and it works as expected even with both filters in place. ... 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. ...
    (microsoft.public.dotnet.languages.vb)
  • Re: FileSystemWatcher Changed Event Double Activation
    ... As I recall I saw this problem when using notepad to add ... as expected even with both filters in place. ... filter to lastwrite, and the changed event still gets called twice. ...
    (microsoft.public.dotnet.languages.vb)
  • Re: FileSystemWatcher Changed Event Double Activation
    ... If you are using notepad, ... I just tested with both filters in place, ... filter to lastwrite, and the changed event still gets called twice. ...
    (microsoft.public.dotnet.languages.vb)
  • Re: OffTopic - Mozilla Thunderbird is not bad
    ... couldn't find a feature in Tbird that does this. ... I like to sort all of the watched messages to the top --even the ... Filters don't seem to work. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: how do I play multiple .ppt files continuously
    ... By default, Notepad saves files as .txt which is just fine, or you can use ... If you use the Package for CD copy to file feature including the viewer, ... this will be done for you by PowerPoint. ... the pptviewer.exe file that will play the .ppt files together. ...
    (microsoft.public.powerpoint)