nikos_22

I have a generic printer that saves a print job to a file "myfile". I want every time that the printer sends a file , to catch this and copy the file into my folder. When i print a test page i receive twice the changed event . Can anyone show me how to manage this issue

Thank You.


Re: .NET Base Class Library filesystemWatcher changed twice

Peter Ritchie

You may get the changed event as much as once per byte written, depending on how the other application is writing to the file. There's no events to tell you when the other application is done writing, so the only thing you can do is guess that it's done.

The most common pattern I've seen for this is to use a Timer of some sort. When a changed event is received the timer is started for some arbitrary amount of time (say 1 second) if another change event does not come in within that time span then it's likely that the other application is done writing to the file for the time being and you can start processing it.

The problem with the FileSystemWatcher is that you can't know the internal implementation of the other applications' IO that you're watching, so you'll have to handle the occaisional situations where the other application begins writting to the file after you've started processing it.