简体   繁体   English

如何以最少的锁定访问文件

[英]How to access a file with the least amount of locking

in my application (c# 3.5) there are various processes accessing a single xml file (read and write) very frequently. 在我的应用程序(c#3.5)中,有很多进程非常频繁地访问单个xml文件(读取和写入)。 the code accessing the file is placed in an assembly referenced by quite a couple of other applications. 访问该文件的代码放在由很多其他应用程序引用的程序集中。 for example a windows service might instanciate the MyFileReaderWriter class (locatied in the previously mentioned assembly) and use the read/write methods of this class. 例如,Windows服务可能会实现MyFileReaderWriter类(在前面提到的程序集中定位)并使用此类的读/写方法。

i'm looking for the best/fastest way to read and create/append the file with the least amount of locking. 我正在寻找最好/最快的方式来读取和创建/追加具有最少量锁定的文件。 caching the files data and flushing new content periodically is not an option, since the data is critical. 由于数据非常重要,因此无法定期缓存文件数据和刷新新内容。

I forgot to mention that I currently use the XDocument (LInq2Xml infrastructure) for reading/writing the content to the file. 我忘了提到我目前使用XDocument(LInq2Xml基础设施)来读取/写入文件内容。

If by writing you're only appending to the document, then try this 如果通过写作你只是附加到文档,那么试试这个

http://msdn.microsoft.com/en-us/library/aa302289.aspx http://msdn.microsoft.com/en-us/library/aa302289.aspx

Basically your xml document is a shell with a start and end tag. 基本上你的xml文档是一个带有开始和结束标记的shell。 Between the tags is an include reference, that references another file, this other file is the body of the xml document. 标签之间是一个include引用,它引用另一个文件,另一个文件是xml文档的主体。 For write operations this is many times faster, because you don't have to load the xml file each time you write to it, and you don't write the entire file each time. 对于写入操作,速度要快很多倍,因为每次写入时都不必加载xml文件,并且每次都不写入整个文件。

Using an XmlWriter class you can very easily append nodes to the end of the body file. 使用XmlWriter类,您可以非常轻松地将节点附加到正文文件的末尾。

When others want to read the xml, they simply open the document, their xml parser "knows" to load and parse both files. 当其他人想要读取xml时,他们只需打开文档,他们的xml解析器“知道”加载和解析这两个文件。

Hope this helps. 希望这可以帮助。

To start with, make sure that all access to the XML Document is done through a single instance. 首先,确保通过单个实例完成对XML Document的所有访问。

Then put a lock mechanism (that wait for unlock) at the beginning of the function that access it and unlock it at the end. 然后在访问它的函数的开头放置一个锁定机制(等待解锁)并在最后解锁它。

What do you mean the data is critical? 你的意思是数据至关重要? Is the XML file not exclusively used by your application? XML文件不是您的应用程序专用的吗?

As simple (and maybe good enough) way to lock IO access to the file would be to create a static factory class that provides a "read write token" object that each I/O operation would have to have a lock on. 简单(也许足够好)锁定对文件的IO访问的方法是创建一个静态工厂类,它提供一个“读写令牌”对象,每个I / O操作都必须锁定。

public static object GetReadWriteToken(String fileName)
{
    //use a hashtable to retreive an object, with filename as key
    return token;
}

and simply do this while reading and writing: 并在阅读和写作时简单地这样做:

lock(GetReadWriteToken(@"C:\important.xml"))
{
    //io code
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM