XmlDocument.Save() does not close the file

L

liuhengyi

Hi,

I have a test program that creates 5 threads and each thread uses
XmlDocument.Save(filename) to save a Xml dom to a file. I have put the
lock statement around the Save to prevent concurrent update. However
I'm still seeing the error "System.IO.IOException: The process cannot
access the file "c:\file1.xml" because it is being used by another
process." Am I missing something?

public class XmlTest
{
private static void ThreadProc(object o)
{
XmlDocument x1 = new XmlDocument();
x1.AppendChild(x1.CreateElement("test"));

while (running)
{
lock (typeof(XmlTest))
{
try
{
x1.Save(@"c:\file1.xml");
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
}
}

//...
}

Thanks,
-hengyi
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top