Multithreading 无法使用lock()或互斥锁锁定xml写入

Multithreading 无法使用lock()或互斥锁锁定xml写入,multithreading,parallel-processing,locking,mutex,Multithreading,Parallel Processing,Locking,Mutex,我正在编写一个简单的应用程序,它从多个线程向单个xml文件写入大约1000次迭代。 我已经尝试使用lock()、Mutex和ReaderWriterSlim在写入文件时锁定文件。它工作了一段时间,但给出了另一个进程正在访问文件的错误信息。 有人能帮我找到一些解决方案吗: 或者有没有其他方法来实现这一点 public class XmlWrite { //private static object _lock = new Object(); static string xmlF

我正在编写一个简单的应用程序,它从多个线程向单个xml文件写入大约1000次迭代。 我已经尝试使用lock()、Mutex和ReaderWriterSlim在写入文件时锁定文件。它工作了一段时间,但给出了另一个进程正在访问文件的错误信息。 有人能帮我找到一些解决方案吗:

或者有没有其他方法来实现这一点

public class XmlWrite
{
    //private static object _lock = new Object();


    static string xmlFilePath = "C:\\Test\\Multi.xml";
    public static Mutex m_Mutex = new Mutex(false, @"Global\MyMutex");

    public static void LogXML(object threadID)
    {
        int i = 0;
        while (i < 1000)
        {
            try
            {
                m_Mutex.WaitOne();
                XmlDocument xml = LoadDocument(xmlFilePath);

                var xmlElement = XElement.Load(new XmlNodeReader(xml));

                var sourceXElement = new XElement("ParentNode");
                sourceXElement.Add(new XElement("ChildNode", threadID));

                xmlElement.Add(sourceXElement);

                SaveDocument(xmlElement, xmlFilePath);

                Console.WriteLine("Value " + xml.ToString() + "Index" + i + "Thread" + threadID.ToString());
            }
            catch (Exception exe)
            {
                Console.WriteLine(exe.Message);
            }
            finally
            {
                m_Mutex.ReleaseMutex();
            }
            i = i + 1;
        }
    }

    public static XmlDocument LoadDocument(String path)
    {
        XmlDocument document = new XmlDocument();
        using (StreamReader stream = new StreamReader(path, Encoding.GetEncoding("iso-8859-7")))
        {
            document.Load(stream);

        }
        return (document);
    }

    public static XElement SaveDocument(XElement document, String path)
    {
        using (StreamWriter stream = new StreamWriter(path, false, Encoding.GetEncoding("iso-8859-7")))
        {
            document.Save(stream);
        }
        return (document);
    }


}


class Program
{
    static void Main(string[] args)
    {


        new Thread(XmlWrite.LogXML).Start("A");
        new Thread(XmlWrite.LogXML).Start("B");
        new Thread(XmlWrite.LogXML).Start("C");
        new Thread(XmlWrite.LogXML).Start("D");
        new Thread(XmlWrite.LogXML).Start("E");
        new Thread(XmlWrite.LogXML).Start("F");
        new Thread(XmlWrite.LogXML).Start("G");
        new Thread(XmlWrite.LogXML).Start("H");
        new Thread(XmlWrite.LogXML).Start("i");
    }
}
公共类XmlWrite
{
//私有静态对象_lock=新对象();
静态字符串xmlFilePath=“C:\\Test\\Multi.xml”;
公共静态互斥体m_Mutex=新互斥体(false,@“Global\MyMutex”);
公共静态void LogXML(对象threadID)
{
int i=0;
而(i<1000)
{
尝试
{
m_Mutex.WaitOne();
XmlDocument xml=LoadDocument(xmlFilePath);
var xmlement=XElement.Load(新的XmlNodeReader(xml));
var sourceXElement=新的XElement(“父节点”);
添加(新的XElement(“ChildNode”,threadID));
Add(sourceXElement);
SaveDocument(xmlement,xmlFilePath);
WriteLine(“Value”+xml.ToString()+“Index”+i+“Thread”+threadID.ToString());
}
捕获(异常exe)
{
Console.WriteLine(exe.Message);
}
最后
{
m_Mutex.ReleaseMutex();
}
i=i+1;
}
}
公共静态XmlDocument LoadDocument(字符串路径)
{
XmlDocument document=新的XmlDocument();
使用(StreamReader stream=newstreamreader(路径,Encoding.GetEncoding(“iso-8859-7”))
{
文件加载(流);
}
报税表(文件);
}
公共静态XElement保存文档(XElement文档,字符串路径)
{
使用(StreamWriter stream=newstreamwriter(路径,false,Encoding.GetEncoding(“iso-8859-7”))
{
文档保存(流);
}
报税表(文件);
}
}
班级计划
{
静态void Main(字符串[]参数)
{
新线程(XmlWrite.LogXML).Start(“A”);
新线程(XmlWrite.LogXML).Start(“B”);
新线程(XmlWrite.LogXML).Start(“C”);
新线程(XmlWrite.LogXML).Start(“D”);
新线程(XmlWrite.LogXML).Start(“E”);
新线程(XmlWrite.LogXML).Start(“F”);
新线程(XmlWrite.LogXML).Start(“G”);
新线程(XmlWrite.LogXML).Start(“H”);
新线程(XmlWrite.LogXML).Start(“i”);
}
}

文件似乎被互斥锁正确保护。您确定没有其他进程(除了上面的代码)在不获取互斥锁的情况下访问该文件吗?可能是有一个编辑器或其他东西正在访问该文件。我改变了方法,甚至现在代码也抛出了异常。我必须从同一个文件读写。还有另一个应用程序/进程可以读取相同的内容。因此,我使用命名互斥体。不确定即使使用互斥多线程写入也会出现相同的异常。