Question: I have a doubt about lock statment

All examples that i see, lock statment is used for global variable in a same form, but in my case that no happend, then i don't know how avoid the error "file is used for another..."

My case.
User go to form1, and in the form1, add lines to XML. While, in second plane, a thread take that XML and send info to server and update state objects in this XML.
Then when user and thread write at the same (one by user and other by second plane, the xml save throw error)
I'm doing that

```
form1
{
  var A = takeAllABCObjects();
  var A = A + new objects();
  saveABCObjects(A);
}

Thread in second plane in all time life of app
{
  var B = takeAllABCObjects();
  B = UpdateObjectsB();
  saveABCObjects(B);
}

Class saveABCObjects(list<objects> ABCObjects)
{
 lock (ABCObjects)
 {
     XmlSerializer serializer = new XmlSerializer(typeof(List<objects>));
     TextWriter textWriter = new StreamWriter("ABC.xml");
     serializer.Serialize(textWriter, ABCObjects);
     textWriter.Close();
 }
}

```

Then my question is...
lock statment use are wrong?
how should i use it?

Tnx and sorry for my eng

Answer: 
I'd go with a `wrapper` class to access your XML

Sonce your two threads do not know when each other is accessing the file let a class do it on it's own.

I was thinking of something like this :

```
public class SynchronizedXMLAccess : IDisposable
{
    private static readonly Mutex _access = new Mutex();
    public FileStream Fs { get; private set; }

    public SynchronizedXMLAccess(String path, FileMode mode = FileMode.Open, FileAccess access = FileAccess.ReadWrite, FileShare sharing = FileShare.None)
    {
        _access.WaitOne();
        Fs = File.Open(path, mode, access, sharing);
    }

    #region Implementation of IDisposable
    public void Dispose()
    {
        Fs.Close();
        _access.ReleaseMutex();
    }

    #endregion
}

```

}

You'll need to instantiate the class to access a file, but it will ensure it is only opened once between the threads. And since it implements `IDisposable` you'll be able to instantiate it with a `using` statement

It's the most basic form, so if you use it, you won't be able to access 2 different files, only one file wll be able to be opened across the threads 

**EDIT :**

Usage example : 

```
Class saveABCObjects(list<objects> ABCObjects)
{
  using(var sync = new SynchronizedXMLAccess(...)) 
  {
    XmlSerializer serializer = new XmlSerializer(typeof(List<objects>));
    TextWriter textWriter = new StreamWriter(sync.Fs);
    serializer.Serialize(textWriter, ABCObjects);
    textWriter.Close();
  }
}

```
if you open the file with write permissions and "read" Sharing rights you should be fine

so processA opens for writeing

```
using(FileStream fs = File.Open("file.ext",FileMode.Open,FileAccess.Write,FileShare.Read))

```

and processB opens for reading

```
using(FileStream fs = File.Open("file.ext",FileMode.Open,FileAccess.Read,FileShare.ReadWrite))

```

adds a bit of code but it should allow one process to write while another is reading

as for the reader not going past the end is a whole other story