c# - Can locking a List fail -


I have a class that receives some buffering from MemoryStream, the class actually works as expected , But now every time I get an invalid operation exception when reading one with an error message

the archive was modified; Calculation work can not be executed.

My code is down and the only line to calculate a collection can be:

  m_buffer = m_buffer.Skip) count .toList (;  

However, I have this and all other actions that can modify the m_buffer object within a lock, so I will explain how to write a writing process due to that exception. Can interfere?

  Public class MyMemoryStream: MemoryStream {Personal Manual ResetEvent m_dataReady = New Man Al-ResetEvent (wrong); Private list & lt; byte & gt; M_buffer = new list & lt; byte & gt; (); Public override zero write (byte [] buffer, int offset, int count) {lock (m_buffer) { M_bufferAdrej (bufferOoList (left) (offset) take (calculation));} M_dataReady.Set ();} Read public override en (byte [] buffer, integer offset, int counting) {if (m_buffer. Count == 0) {// Some other figures in the stream till block are m_dataReady.Reset (); m_dataReady.WaitOne ();} lock (m_buffer) {if (m_buffer. Mount> gt; = count) {// more bytes Of the The request was made in Ullana. Array.Copy (m_buffer.ToArray (), 0, buffer, offset, count); M_buffer = m_buffer.Skip (count). Olist (); Calculation of return; } And {int length = m_buffer.Count; Array.Copy (M_buffer .ORRU (), 0, buffer, offset, length); M_buffer.Clear (); Return length; }} 

I can not say what is going wrong with the code you posted, But there is a bit of an inequality that you lock on M_buffer, but instead of buffer, the collection lock is not always stored, which is read and modified.

This is a private readonly object for a dedicated locking:

  Private Readonly Object Locker = New Object (); // ... lock (locker) {// ...}  

Comments

Popular posts from this blog

c# - How to capture HTTP packet with SharpPcap -

jquery - SimpleModal Confirm fails to submit form -

php - Multiple Select with Explode: only returns the word "Array" -