读取\写入文件:使用频繁的应用程序

时间:2013-07-23 04:23:55

标签: c# asp.net .net concurrency

我在这里问了一个问题:The process cannot access the file because it is being used by another process: Correct way to read\write to a file: heavily used application-Part II

我们有一个使用频繁的.Net 3.5应用程序,其中包含“创建成本高昂”的应用程序。数据和缓存它。但是,我们在读取缓存文件和写入缓存文件时遇到很多错误。我有一个进程,多个线程,我希望应用程序同步对资源的访问。我被建议使用一个简单的锁定机制,如lock或ReaderWriterLockSlim(参见:http://msdn.microsoft.com/en-us/library/system.threading.readerwriterlockslim.aspx)。然而,这似乎使问题在生产中更加严重。

修改 实施更改后,许多缓存文件都有一个">"标签到底。因此,文件不再是xml文件。

有人可以查看代码并告知我可能做错了什么吗?

更改前的代码:

private XmlDocument ReadFromFile()
{
    XmlDocument result=null;
    string fileSystemPath=FileSystemPath();
    try
    {
        result=new XmlDocument();
        using (StreamReader streamReader = new StreamReader(fileSystemPath))
        {
            result.Load(streamReader);
        }
    }
    catch (FileNotFoundException)
    {
        result=null;
    }
    return result;
}

private object thisObject= new object();
private void WriteToFile(string siteID, XmlDocument stuff)
{
    string fileSystemPath=FileSystemPath();
    lock(thisObject)
    {
        using (StreamWriter streamWriter = new StreamWriter(fileSystemPath))
        {
            stuff.Save(streamWriter);
        }
    }
}

更改后的代码:

private readonly ReaderWriterLockSlim readerWriterLockSlim = new ReaderWriterLockSlim();
private XmlDocument ReadFromFile()
{
    XmlDocument result = null;
    var fileSystemPath = FileSystemPath();          
    readerWriterLockSlim.EnterReadLock();
    try
    {
        result = new XmlDocument();
        using (var fileStream = new FileStream(fileSystemPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
        using (var streamReader = new StreamReader(fileStream))
        {
            result.Load(streamReader);
        }
    }
    catch (FileNotFoundException)
    {
        result = null;
    }
    finally
    {
        readerWriterLockSlim.ExitReadLock();
    }
    return result;
}


private void WriteToFile()
{
    var fileSystemPath = FileSystemPath();            
    readerWriterLockSlim.EnterWriteLock();
    try
    {
        using (var fileStream = new FileStream(fileSystemPath, FileMode.OpenOrCreate, FileAccess.Write, FileShare.ReadWrite))
        using (var streamWriter = new StreamWriter(fileStream))
        {
            stuff.Save(streamWriter);
        }
    }
    finally
    {
        readerWriterLockSlim.ExitWriteLock();
    }
}

1 个答案:

答案 0 :(得分:0)

此代码稍作修改应该有效

private readonly ReaderWriterLockSlim readerWriterLockSlim = new ReaderWriterLockSlim();
private XmlDocument ReadFromFile()
{
    XmlDocument result = null;
    var fileSystemPath = FileSystemPath();          
    readerWriterLockSlim.EnterReadLock();
    try
    {
        result = new XmlDocument();
        using (var streamReader = new StreamReader(fileStream))
        {
            result.Load(streamReader);
        }
    }
    catch (FileNotFoundException)
    {
        result = null;
    }
    finally
    {
        readerWriterLockSlim.ExitReadLock();
    }
    return result;
}


private void WriteToFile(XmlDocument stuff)
{
    var fileSystemPath = FileSystemPath();            
    readerWriterLockSlim.EnterWriteLock();
    try
    {
        using (var streamWriter = new StreamWriter(fileSystemPath))
        {
            stuff.Save(streamWriter);
        }
    }
    finally
    {
        readerWriterLockSlim.ExitWriteLock();
    }
}