Reputation: 1293
I'm having a problem working with filesystemwatcher, it's driving me crazy.
Turns out I'm watching for new text files in a folder, when the Created Event is raised, I basically read it using the following code:
string txtTemp = File.ReadAllText(MyFilePath);
After that, I process the data in the txtTemp string, basically I read it's lines & store the data on a DB, pretty simple isn't it?
The problem is when an exception is raised in this process (let's say db connection failed), it doesn't matter if I catch it because for the next coming files the app will throw an exception saying
"The process cannot access the file 'theNewComingFile.txt' because it is being used by another process."
How come the new created file could be in use if it hasn't even been opened or read it? And the application keeps throwing this "process cannot access the file" exception for all new files.
The only thing we can do is to close and reopen the application, this resets the app and everything works fine again until an exception of any type is raised again> (gosh!)
Any ideas? any workaround? any thoughts? any suggestions any... anything? hehehehe =)
Thanks dudes !!
Upvotes: 5
Views: 2216
Reputation: 4851
The Created event is fired before the file is finished being completely written.
From MSDN:
The OnCreated event is raised as soon as a file is created. If a file is being copied or transferred into a watched directory, the OnCreated event will be raised immediately, followed by one or more OnChanged events.
So getting the exception you describe would be fairly normal. When I've faced this situation, I basically test the file first to see if I can access it or not. Here is the function I use:
private static bool GetExclusiveAccess(string filePath)
{
try
{
using (FileStream file = new FileStream(filePath, FileMode.Append, FileAccess.Write))
{
file.Close();
return true;
}
}
catch (IOException)
{
return false;
}
}
If you don't have access, then you'll need to wait and check again.
Upvotes: 2
Reputation: 2946
you can start your investigation using the Sysinternals Process Explorer. It will give you more details, particularly on which process holds the file.
Upvotes: 5
Reputation: 25200
It may be the act of opening the file changes the last-read timestamp: this could cause a loop.
You might want to try changing your design so that the events are placed onto a queue if (and only if) the file isn't already queued for processing. You can then trap for exceptions, etc., under proper control on the other thread without getting in the way of the Watcher.
[I've seen some very interesting corner cases with FileSystemWatcher
in the past - it can be a bit of a beast when cornered. One (a while back) caused a not-quite-reproducible blue-screen hang in Windows Server 2003 when an internal buffer overflowed because it was swamped with events. Best be careful.]
Upvotes: 2
Reputation: 39013
A work-around is simple - put your watcher in its own AppDomain, tear down the AppDomain and restart if you handle an exception. This will most probably fix your problem. But why does it happen? I'll have to think about it a little, and maybe find something.
Upvotes: -1