FileWatcher

B

ba.hons

Hello All.

I have a filewatcher object which looks at a shared folder and when a
new XML file is placed in this folder the on create method calls a
method which reads the XML file and based on the content writes a new
XML file to the same directory.

The problem i have is as follows.

If i place 10 XML files in the directory name

test1.xml
test2.xml
etc
test10.xml

then i will get 10 response XML files, BUT only on the first attempt.

If i then paste another 10 XML files in the directory

test11.xml
test12.xml
etc
test20.xml

i will only receive 9 XML response files, this is because the first XML
file from the new batch (test11.xml in this case) always fails. I
always get an exception thrown when i try to read the first file, the
rest all work, but if i repeat the process it happens again, excpetion
is thrown when i try to read the first XML file, the next 9 read
correctly and then get a response.

I dont see why this is happening, i make sure to close the
IO.filestreams and it doesnt happend for every read, i make a new
object each time the create method fires (which is 10 times for this
example) so if anything it should fail 10 times or never.


My code is as follows:

private void OnXMLFileCreated(object source, FileSystemEventArgs e)
{
XMLFileEventMng test = new XMLFileEventMng();
try
{
test.HandleEvent(e.FullPath.ToLower());
}
catch(Exception ex)
{
MessageBox.Show(ex.ToString());
}
}

In the XMLFileEventMng when i do

System.IO.FileStream fsReadXml = new System.IO.FileStream (xmlFileName,
FileMode.Open);

I receive this:

System.IO.IOException: The process cannon access the file "test11.xml"
because it is being used by another process.


If anyone has any ideas on this i would really appreciate them.

Thanks

Adam
 
B

ba.hons

I Have found a workaround for this as follows:

try
{
fsReadXml = new System.IO.FileStream (xmlFileName, FileMode.Open);
}
catch(Exception ex)
{
fsReadXml = new System.IO.FileStream (xmlFileName, FileMode.Open);
}

This seems to solve the problem of the error being thrown, but i would
much rather figure out what is actually causing the Exception as this
is a very messy way to deal with this.

thanks

Adam
 
B

Bobbo

ba.hons said:
I have a filewatcher object which looks at a shared folder
I always get an exception thrown when i try to read the first file

I had a similar problem recently. In my case it was because the file
creation event that the app receives seems to happen before the file
handle is closed by whatever is generating them. I'm guessing a delay
is more likely on a share.

To work around this I implemented a waiting system (if anyone has any
better suggestions feel free) along the following lines:

public static bool CheckFileAvailable(string filePath)
{
bool isAvailable = false;
try
{
using (FileStream inputStream = File.Open(filePath,
FileMode.Open, FileAccess.Read, FileShare.None))
{
isAvailable = true;
}
}
catch (IOException)
{
isAvailable = false;
}
return isAvailable;
}

This is just a single 'available?' check, and In my case I expanded
this to a 'try 3 times' type loop with a Thread.Sleep() call on each
iteration. Note that only IOException is caught rather than Exception,
to allow other exceptions to propagate up through the call stack
properly.
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

|
| ba.hons wrote:
|
| > I have a filewatcher object which looks at a shared folder
| > I always get an exception thrown when i try to read the first file
|
| To work around this I implemented a waiting system (if anyone has any
| better suggestions feel free) along the following lines:

I also got into the same problem, I did something else, not sure if it work
in your environment but anyway here it goes

As soon as I receive an event I put it in a Sync'ed queue. Then I have a
worker thread devoted to process those request, it;s mainly a loop where it
check if there is something in the queue if not it goes to sleep X seconds
until it poll again the queue, if there is something in the queue I use Peek
to see if I can access the file. If I get access error probably is cause the
originator is still writting it (they are some big XML files). so I go back
to sleep. Once I can read it I just DeQueue it and do the processing.

Now the FileSystemWatcher may generate more than one event for a
"modification" in my case I can avoid reprocessing cause I delete the file
after processing. so the first thing I do when I Dequeue it is checking if
the file still exist.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top