Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 482119
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T01:03:50+00:00 2026-05-13T01:03:50+00:00

I have a class that inherits from MemoryStream in order to provide some buffering.

  • 0

I have a class that inherits from MemoryStream in order to provide some buffering. The class works exactly as expected but every now and then I get an InvalidOperationException during a Read with the error message being

Collection was modified; enumeration operation may not execute.

My code is below and the only line that enumerates a collection would seem to be:

m_buffer = m_buffer.Skip(count).ToList();

However I have that and all other operations that can modify the m_buffer object within locks so I’m mystified as to how a Write operation could interfere with a Read to cause that exception?

public class MyMemoryStream : MemoryStream
{
    private ManualResetEvent m_dataReady = new ManualResetEvent(false);
    private List<byte> m_buffer = new List<byte>();

    public override void Write(byte[] buffer, int offset, int count)
    {
        lock (m_buffer)
        {
            m_buffer.AddRange(buffer.ToList().Skip(offset).Take(count));
        }
        m_dataReady.Set();
    }

    public override int Read(byte[] buffer, int offset, int count)
    {
        if (m_buffer.Count == 0)
        {
            // Block until the stream has some more data.
            m_dataReady.Reset();
            m_dataReady.WaitOne();
        }

        lock (m_buffer)
        {
            if (m_buffer.Count >= count)
            {
                // More bytes available than were requested.
                Array.Copy(m_buffer.ToArray(), 0, buffer, offset, count);
                m_buffer = m_buffer.Skip(count).ToList();
                return count;
            }
            else
            {
                int length = m_buffer.Count;
                Array.Copy(m_buffer.ToArray(), 0, buffer, offset, length);
                m_buffer.Clear();
                return length;
            }
        }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T01:03:51+00:00Added an answer on May 13, 2026 at 1:03 am

    I cannot say exactly what’s going wrong from the code you posted, but a bit of an oddity is that you lock on m_buffer, but replace the buffer, so that the collection locked is not always the collection that is being read and modified.

    It is good practice to use a dedicated private readonly object for the locking:

    private readonly object locker = new object();
    
        // ...
        lock(locker)
        {
             // ...
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Newbie Python question. I have a class that inherits from several classes, and some
I have a class that inherits from an abstract base class. I am trying
I have a class that inherits from X509Certificate2. I want the NotAfter property to
I have the following situation. I have a Java Class that inherits from another
I have a base class, SpecialClass . Lots of other classes inherit from it,
Hey. I have a list of items that the user can search. The search
In one C# project have a base class like this: public abstract class AbstractVersionedBase
I have a web service and multiple clients that will use it. A MVC
public partial class Form1 : Form What does the partial in this declaration mean?
I'm facing a problem with reflection in c# and I can't find the answer.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.