If the FileSystemWatcher is running in asynchronous mode, events will fire when they happen, and if the handler relies on having an exclusive lock, you'll get an error. Here's the kicker though: when the changed event is fired and you open the file, the access time will change and fire the event again.
Your best bet in this case would be to treat the FileSystemWatcher like a timer. When it ticks, stop it long enough to do what you want to do, then start it again.
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
>What meens by this. What is an exclusive lock ?
It means the file can only be opened by one thread at a time. Exclusive access is the default open mode, but you can change that to shared if you need to. However, having one thread read from the file and another write to the file simultaneously is rather difficult to get right.
>I did this only to test how this works but I get the
>same error here that the file is in use by another process.
I'd guess that whoever is writing the file isn't done yet. Try waiting for 30 seconds or so and then read the file. If it works that's probably the problem.
>NotifyFilter is "LastWrite"
Nevermind then. Your event handler shouldn't cause the changed event to fire unless you also poll for the last access.
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
>How could the second shoot be prevented.
You need to figure out where it's coming from, then if you have control over that part, change it so that the second event isn't fired. Otherwise, write your event handler so that it doesn't choke on events in quick succession.
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
As I have put the filter to "LastWrite" why does the event shoot 2 times because I limit it to just LastWrite and are not using the "LastAccess" also.
I guess that your editor makes calls to Windows API functions that cause the event to occur twice. You might write a simple test program which does a fopen()/fwrite()/fclose() sequence, the event probably fires only once in that case.
I have googled around about this and it seems to be a problem but cant find any solution or good explanation what really happens in this case.
If you want to know what actually takes place when you modify the file, then you might find Process Monitor from SysInternals quite useful. You'll be able to easily capture all related filesystem events.
mitrmkar
Posting Virtuoso
1,809 posts since Nov 2007
Reputation Points: 1,105
Solved Threads: 395