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: 
The basic problem is that your thread and form are not using the same lock object. I don't know how you are creating your thread but I've written up a little console app with 2 tasks that will lock and alternately write and read to a file. You have to pass in the same lock object to each task in order for this to work.

```
static void Main(string[] args)
{
    var lockObject = new object();
    var fileName = @"C:\Users\kevin\Documents\test.txt";
    Action<object> action1 = (o) =>
    {
        var i = 0;
        while (i++ < 1000)
        {
            // do stuff that doesn't require the lock
            lock (o)
            {
                Console.WriteLine("In Thread1");
                // do stuff that does require the lock
                var text = File.ReadAllText(fileName);
                Console.WriteLine(text);
                File.WriteAllText(fileName, "\tThread1");
            }
        }
    };
    // Pass in our shared lock object
    Task task1 = new Task(action1, lockObject);
    task1.Start();
    Action<object> action2 = (o) =>
    {
        var i = 0;
        while (i++ < 1000)
        {
            // do stuff that doesn't require the lock
            lock (o)
            {
                // do stuff that does require the lock
                Console.WriteLine("In Thread2");
                var text = File.ReadAllText(fileName);
                Console.WriteLine(text);
                File.WriteAllText(fileName, "\tThread2");
            }
        }
    };
    // Pass in our shared lock object
    Task task2 = new Task(action2, lockObject);
    task2.Start();

    // sleep main thread and let the 2 threads do their stuff
    Thread.Sleep(5000);

    Console.ReadLine();
}

```
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