FileWatcher with XMLTextReader

G

Guest

I have a small problem with my windows service. The first time I start the service everything works the way it's suppose to, but randomly the service fails. The jist of the service is to use the filewatcher component to watch a directory. On create only, the service will read the small XML file, process the information. Once the file is finished processing the information, it should copy the file to another directory and delete the original.

After the first file is processed by the filewatcher, the following error message occurs on all the files after "The process cannot access the file "C:\test\rk.xml" because it is being used by another process".

Can someone tell me why the file is not being released? Any help would be greatly appreciated! Please see the code snippet below:

protected void fsw_Created (
object sender, System.IO.FileSystemEventArgs e)
{

XmlTextReader reader = new XmlTextReader (e.FullPath);


//Declare Array
String[] a = new String[10];
int arrayCounter = 0;


try
{

while (reader.Read())
{


switch (reader.NodeType)
{
// case XmlNodeType.Element: // The node is an element.
// Console.Write("<" + reader.Name);
// Console.WriteLine(">");
// break;
case XmlNodeType.Text: //Display the text in each element.
// Console.WriteLine (reader.Value);
a[arrayCounter] = reader.Value;
arrayCounter += 1;
break;
// case XmlNodeType.EndElement: //Display the end of the element.
// Console.Write("</" + reader.Name);
// Console.WriteLine(">");
// break;


//End of switch
}


//End of while
}


if (reader.EOF == true)

{
reader.Close();

}
string prcinstance = a[0];
string[] actionNames = new string[a.Length -1];
Array.Copy(a,1,actionNames,0,9);



//File.Delete(e.FullPath);
File.Copy(e.FullPath,"c:\\" + e.Name,true);
EventLog.WriteEntry("Monitor", e.Name + " has been copied");



if (File.Exists("c:\\" + e.Name))

{


File.Delete(e.FullPath);

EventLog.WriteEntry("Monitor", e.Name + " has been copied");


}




}

catch (Exception ex)
{
EventLog.WriteEntry("Monitor - Error", ex.Message);

}



}




Thanks,
Ron
 
K

Klaus H. Probst

Perhaps you're getting an exception and you're not closing the reader? Maybe
using a finally block and closing the reader there would help.

i.e.,

finally
{
if (reader != null)
reader.Close();
}

Also I didn't look too closely at your code but this:

if (reader.EOF)
{
reader.Close();
}

seems kind of iffy? Why not just exit the loop and close it? It seems kind
of pointless to check for EOF and then close.

--
____________________
Klaus H. Probst, MVP
http://www.vbbox.com/

Ron King Jr said:
I have a small problem with my windows service. The first time I start the
service everything works the way it's suppose to, but randomly the service
fails. The jist of the service is to use the filewatcher component to watch
a directory. On create only, the service will read the small XML file,
process the information. Once the file is finished processing the
information, it should copy the file to another directory and delete the
original.
After the first file is processed by the filewatcher, the following error
message occurs on all the files after "The process cannot access the file
"C:\test\rk.xml" because it is being used by another process".
Can someone tell me why the file is not being released? Any help would be
greatly appreciated! Please see the code snippet below:
protected void fsw_Created (
object sender, System.IO.FileSystemEventArgs e)
{

XmlTextReader reader = new XmlTextReader (e.FullPath);


//Declare Array
String[] a = new String[10];
int arrayCounter = 0;


try
{

while (reader.Read())
{


switch (reader.NodeType)
{
// case XmlNodeType.Element: // The node is an element.
// Console.Write("<" + reader.Name);
// Console.WriteLine(">");
// break;
case XmlNodeType.Text: //Display the text in each element.
// Console.WriteLine (reader.Value);
a[arrayCounter] = reader.Value;
arrayCounter += 1;
break;
// case XmlNodeType.EndElement: //Display the end of the element.
// Console.Write("</" + reader.Name);
// Console.WriteLine(">");
// break;


//End of switch
}


//End of while
}


if (reader.EOF == true)

{
reader.Close();

}
string prcinstance = a[0];
string[] actionNames = new string[a.Length -1];
Array.Copy(a,1,actionNames,0,9);



//File.Delete(e.FullPath);
File.Copy(e.FullPath,"c:\\" + e.Name,true);
EventLog.WriteEntry("Monitor", e.Name + " has been copied");



if (File.Exists("c:\\" + e.Name))

{


File.Delete(e.FullPath);

EventLog.WriteEntry("Monitor", e.Name + " has been copied");


}




}

catch (Exception ex)
{
EventLog.WriteEntry("Monitor - Error", ex.Message);

}



}




Thanks,
Ron
 
G

Guest

Thanks for the repsonse Klaus

I added the finally statement to the code and removed the reader.EOF check. The service is running better, but I still randomly get the error with a process holding on the file. We I drop new files with different names sometimes it processes. Any other advice you can share

finall


if (reader != null

reader.Close()


File.Move(e.FullPath,"c:\\" + e.Name)








Thanks again!
 
L

Lloyd Sheen

I have a service with FileWatcher and had to implement the dealing of files
to a timer. When I drag/drop a file it sends about 4-5 events so it seems
that when the first event fires and I try to deal with the file there is
still activity on the file. I get around this by using a timer (not a
forms.timer).

Lloyd Sheen

Ron King Jr said:
Thanks for the repsonse Klaus!

I added the finally statement to the code and removed the reader.EOF
check. The service is running better, but I still randomly get the error
with a process holding on the file. We I drop new files with different
names sometimes it processes. Any other advice you can share?
 
G

Guest

Lloyd

Is it psssible to provide some code snippet for the placement of the timer? Also, how long did your timer wait before processing again

Thanks for your help!
 

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

Top