I have a file watcher appliation that on occassion encounters a contention issue with the file that it needs to read. I have added a some code to deal with that in the form of
System.Threading.Thread.Sleep(2000);
Here is the code I am using to get the information I need from the file
using (System.Xml.XmlReader reader = System.Xml.XmlReader.Create(fullFilePath))
{
XDocument doc = XDocument.Load(reader);
reader.Close();
reader.Dispose();
foreach (XElement el in doc.Root.Elements())
{
foreach (XElement element in el.Elements())
{
if (element.Name == "var1")
{
aVal = element.Value.ToString();
}
if (element.Name == "var2")
{
bVal= element.Value.ToString();
}
}
}
}
My question is this: Is there a quicker way to read the contents of the file, there by releasing any lock my application my have on the file?
I updated the code to read from the file as follows:
lock(_lock)
{
using (XmlTextReader xmlTextReader = new XmlTextReader(fullFilePath))
{
XDocument doc = XDocument.Load(xmlTextReader);
foreach (XElement el in doc.Root.Elements())
{
foreach (XElement element in el.Elements())
{
if (element.Name == "AccessionNumber")
{
accessionNumber = element.Value.ToString();
}
if (element.Name == "PatientID")
{
patientID = element.Value.ToString();
}
}
}
}
}
This works great in the debugger, but does not work when used via a release built. Any thoughts?