I am trying to use 1 lock object and trying to write to the same file using StreamWriter in two different places under the lock but I am getting the error:
The process cannot access the file 'filename' because it is being used by another process.
Lock object defined, I used a static object:
protected static Object Semaphore = new Object();
if(x != null)
{
lock (Semaphore)
{
using (StreamWriter sw = new StreamWriter(fileName))
{
sw.WriteLine(a + "," + b + "," + c);
sw.Close();
sw.Flush();
}
}
}
Right after this:
if(y != null)
{
lock (Semaphore)
{
using (StreamWriter sw1 = new StreamWriter(fileName))
{
sw1.WriteLine(a + "," + b + "," + c);
sw1.Close();
sw1.Flush();
}
}
}
What am I missing? Thank you.