string filepath = Environment.CurrentDirectory + @"Expense.xml";
public void WriteToXML(object param)
{
Expense exp = (Expense)param;
if (File.Exists(filepath)) {
XDocument xDocument = XDocument.Load(filepath);
XElement root = xDocument.Element("Expenses");
IEnumerable<XElement> rows = root.Descendants("Expense");
XElement firstRow = rows.First();
firstRow.AddBeforeSelf(new XElement("Expense",
new XElement("Id", exp.Id.ToString()),
new XElement("Amount", exp.Amount.ToString()),
new XElement("Contact", exp.Contact),
new XElement("Description", exp.Description),
new XElement("Datetime", exp.Datetime)));
xDocument.Save(filepath);
}
}
Expense exp = new Expense();
exp.Id = new Random().Next(1, 10000);
exp.Amount = float.Parse(text1[count].Text);
exp.Contact = combo1[count].SelectedItem.ToString();
exp.Description = rtext1[count].Text.ToString();
exp.Datetime = DateTime.Now.ToString("MM-dd-yyyy");
workerThread = new Thread(newParameterizedThreadStart(WriteToXML));
workerThread.Start(exp); // throws System.IO.IOException
我无法使用辅助程序写入XML文件-我遇到此错误:
System.IO.IOException:'该进程无法访问文件'C:\ work \ FinanceManagement \ FinanceManagement \ bin \ DebugExpense.xml',因为该文件正在被另一个进程使用。
但是如果我像WriteToXML(exp);
那样使用它,它将起作用。我认为XDocument.Load(filepath)
不是线程安全的。我该如何解决这个问题?
答案 0 :(得分:3)
尝试引入lock
,看看它是否可以解决问题:
// Declare this somewhere in your project, can be in same class as WriteToXML
static object XmlLocker;
然后将lock
换成逻辑:
public void WriteToXML(object param)
{
Expense exp = (Expense)param;
lock (XmlLocker) // <-- this limits one thread at a time
{
if (File.Exists(filepath))
{
XDocument xDocument = XDocument.Load(filepath);
XElement root = xDocument.Element("Expenses");
IEnumerable<XElement> rows = root.Descendants("Expense");
XElement firstRow = rows.First();
firstRow.AddBeforeSelf(new XElement("Expense",
new XElement("Id", exp.Id.ToString()),
new XElement("Amount", exp.Amount.ToString()),
new XElement("Contact", exp.Contact),
new XElement("Description", exp.Description),
new XElement("Datetime", exp.Datetime)));
xDocument.Save(filepath);
}
}
}