Note that there are some explanatory texts on larger screens.

plurals
  1. PORead\write to a file: heavily used application
    primarykey
    data
    text
    <p>I had asked a question here: <a href="https://stackoverflow.com/questions/16173997/the-process-cannot-access-the-file-because-it-is-being-used-by-another-process">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</a>.</p> <p>We have a heavily used .Net 3.5 application that reads "expensive to create" data and caches it. However we are getting a lot of errors around both reading the cache file and writing to the cache file. I have a single process, multiple threads and I want the application to synchronize access to a resource. I was advised to use a simple locking mechanism like lock or ReaderWriterLockSlim (see: <a href="http://msdn.microsoft.com/en-us/library/system.threading.readerwriterlockslim.aspx" rel="nofollow noreferrer">http://msdn.microsoft.com/en-us/library/system.threading.readerwriterlockslim.aspx</a>). This however seems to have made the problem much much worse in production. </p> <p><strong>EDIT</strong> After the change was implemented, a lot of the cache files have a ">" tag in the end. Due to this the files are no longer xml files.</p> <p>Can someone look at the code and advise what could I be doing wrong?</p> <p><strong>Code before change:</strong></p> <pre><code>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); } } } </code></pre> <p><strong>Code after change:</strong></p> <pre><code>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(); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload