1

I am currently trying to read and write data to a xml file in c# using thread, I have a utilty class that has the function to read the XML file and then write the data into the xml. The way im doing it is as follows.

public class XMLUtil
{

 String lockingObject = "";

 public bool WriteToXml(Product product)
 {
   lock(lockingObject)
   {
       XDocument xDoc = XDocument.Load("file.xml")
   }

   //Code to add data to XElement

   xDoc.Add(xElement)
   lock(lockingObject)
   {
       XDocument xDoc = XDocument.Save("file.xml")
   }
 }

}


public class SomeOtherClass
{

   XMLUtil xmlUtil = new XMLUtil();

   private async void btn_Click(object sender, EventArgs e)
   {
     bool t = await Task.Run(() => xmlUtil.WriteToXml());
   }
}
static class Program
{

// default methods

while (true)
{
   t = await Task.Run(() => XMLUtil.CheckChanges());
   await Task.Delay(20000);
}


}

I also have a background thread that keeps checking for the changes in the XML file to reload the view to the user, because of that I still get the IO/Exception saying that the file is already been used so it cannot be open... How can I fix it?

Pavel Anikhouski
  • 21,776
  • 12
  • 51
  • 66
Rasheen Ruwisha
  • 211
  • 3
  • 9
  • How does `CheckChanges` work? If it is reading and comparing, it is opening the same file, then you need to use the same lock. However, you could use `FileSystemWatcher` and save some trouble. – Theraot Dec 10 '19 at 19:58
  • You really need to address this either globally in your application (using a shared, static lock object), or by using the actual file locking and file sharing APIs provided in .NET (e.g. https://stackoverflow.com/a/3448307/224370). – Ian Mercer Dec 10 '19 at 20:04
  • 2
    CheckChanges() should lock on the same lockingObject instance as WriteToXml() does. Also, Pavel's answer below makes a valid point. However, trying to get exclusive access to the file itself and use that as the main locking mechanism should be the thing to go for. – Cosmin Sontu Dec 10 '19 at 20:05
  • @Theraot The checkChanges method access the file and checks for the number of nodes every 20 seconds and then closes it – Rasheen Ruwisha Dec 10 '19 at 20:16
  • Why do this. Why not when you update the file, update view? You shouldn't have to sleep or poll to check this. – Trevor Dec 10 '19 at 20:26
  • @CosminSontu would I be okay If I make both the methods static and use the locking object as Pavel mentioned below? – Rasheen Ruwisha Dec 10 '19 at 20:26
  • @Çöđěxěŕ This is for a cw and it mentions that the user can manually change the xml file and when it happens the ui should be updated.... – Rasheen Ruwisha Dec 10 '19 at 20:32
  • @Rasheen Ruwisha, making methods static won't make a difference. What is more important is to handle the situations where file read / write attempts fail due to file already being locked by other threads. – Cosmin Sontu Dec 11 '19 at 19:27

1 Answers1

2

Strings are interned in .NET (MSDN reference), you don't have to use them as a lock constructions. Try to define your lockingObject as

private static object lockingObject = new object();

And use it inside lock

Pavel Anikhouski
  • 21,776
  • 12
  • 51
  • 66