0

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?

Neo
  • 3,309
  • 7
  • 35
  • 44
  • 2
    You can copy the file and then read the copied one instead of the original – Gilad Green Oct 04 '16 at 17:23
  • @MisterPositive, have you tried profiler to get the problem places? Also using System.Threading.Thread.Sleep(2000); isn't good practice. Instead of it use Mutex or EventWaitHandle – Artavazd Balayan Oct 04 '16 at 17:33
  • @ArtavazdBalayan could you provide a code sample for EventWaitHandle? This was a one off application I was asked to write -- my background is in ASP.NET / MVC – Neo Oct 04 '16 at 17:34
  • @MisterPositive, sure, but give me more details. Do you have sources to apps which creates and reads this file? – Artavazd Balayan Oct 04 '16 at 17:41
  • I do not sadly. This file is created by another third party application, and when this file is created or changed, my application needs to take action at that point. – Neo Oct 04 '16 at 17:43
  • 1
    @MisterPositive, so there is the chance that while you're reading the file, another app can change it? – Artavazd Balayan Oct 04 '16 at 17:46
  • There is a chance, but its very slight. ( like less than 1% ) In this case I am ok with throwing a message into the application log. – Neo Oct 04 '16 at 17:48
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/124929/discussion-between-artavazd-balayan-and-misterpositive). – Artavazd Balayan Oct 04 '16 at 17:50
  • Why the down votes? I showed code and asked a very specific question. Sometimes I don't get some of the users of this site. – Neo Oct 05 '16 at 12:44

1 Answers1

0

Finally got the issue resolved by implementing retry mechanism for the file locking from this post, which was the reason I asked the original question of efficiency.

FileStream and a FileSystemWatcher in C#, Weird Issue "process cannot access the file"

the answer for me was provided by @Dag on the post above.

Community
  • 1
  • 1
Neo
  • 3,309
  • 7
  • 35
  • 44