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 6150977
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T19:42:04+00:00 2026-05-23T19:42:04+00:00

I have an enumerable that takes a long time to get the next value.

  • 0

I have an enumerable that takes a long time to get the next value.

I’m trying to wrap that enumerable so that I get an enumerable that caches the results.

I’d also like it to do additional loading on another thread (reporting it reached the end of the collection). i.e. if it has 10 cached values, and i enumerate it reports 10, then starts a thread to get the next one so when enumerated again there’s 11 cached values.

What i have so far is below (with code that tests it at the bottom):

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace ConsoleApplication1
{
    public class CachedEnumerable<T> : IEnumerable<T>
    {
        public class CachedEnumerator : IEnumerator<T>
        {
            private IEnumerator<T> _UnderlyingEnumerator;

            public event EventHandler Disposed;

            public CachedEnumerator(IEnumerator<T> UnderlyingEnumerator)
            {
                _UnderlyingEnumerator = UnderlyingEnumerator;
            }

            public T Current
            {
                get { return _UnderlyingEnumerator.Current; }
            }

            public void Dispose()
            {
                _UnderlyingEnumerator.Dispose();

                if (Disposed != null)
                    Disposed(this, new EventArgs());
            }

            object System.Collections.IEnumerator.Current
            {
                get { return _UnderlyingEnumerator.Current; }
            }

            public bool MoveNext()
            {
                return _UnderlyingEnumerator.MoveNext();
            }

            public void Reset()
            {
                _UnderlyingEnumerator.Reset();
            }
        }

        // The slow enumerator.
        private IEnumerator<T> _SourceEnumerator;

        // Whether we're currently already getting the next item.
        private bool _GettingNextItem = false;

        // Whether we've got to the end of the source enumerator.
        private bool _EndOfSourceEnumerator = false;

        // The list of values we've got so far.
        private List<T> _CachedValues = new List<T>();

        // An object to lock against, to protect the cached value list.
        private object _CachedValuesLock = new object();

        // A reset event to indicate whether the cached list is safe, or whether we're currently enumerating over it.
        private ManualResetEvent _CachedValuesSafe = new ManualResetEvent(true);

        public CachedEnumerable(IEnumerable<T> Source)
        {
            _SourceEnumerator = Source.GetEnumerator();
        }

        private void Enum_Disposed(object sender, EventArgs e)
        {
            // The cached list is now safe (because we've finished enumerating).
            _CachedValuesSafe.Set();

            if (!_EndOfSourceEnumerator && !_GettingNextItem)
            {
                _GettingNextItem = true;

                ThreadPool.QueueUserWorkItem((SourceEnumeratorArg) =>
                {
                    var SourceEnumerator = SourceEnumeratorArg as IEnumerator<T>;

                    if (SourceEnumerator.MoveNext())
                    {
                        _CachedValuesSafe.WaitOne(); // Wait for any enumerator to finish

                        lock (_CachedValuesLock)
                        {
                            _CachedValues.Add(SourceEnumerator.Current);
                        }
                    }
                    else
                    {
                        _EndOfSourceEnumerator = true;
                    }

                    _GettingNextItem = false;

                }, _SourceEnumerator);
            }
        }

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return GetEnumerator();
        }

        public IEnumerator<T> GetEnumerator()
        {
            lock (_CachedValuesLock)
            {
                var Enum = new CachedEnumerator(_CachedValues.GetEnumerator());

                Enum.Disposed += new EventHandler(Enum_Disposed);

                _CachedValuesSafe.Reset();

                return Enum;
            }
        }
    }



    class Program
    {
        public static IEnumerable<int> SlowNumbers()
        {
            int i = 0;

            while (true)
            {
                Thread.Sleep(1000);
                yield return i++;
            }
        }

        static void Main(string[] args)
        {
            var SlowNumbersEnumerator = new CachedEnumerable<int>(SlowNumbers());

            while (true)
            {
                foreach (var i in SlowNumbersEnumerator)
                {
                    Console.WriteLine(i);
                    Thread.Sleep(100);
                }
            }
        }
    }
}

My problem is that i get the The collection has been modified error, because the worker thread is adding to the list while it’s being enumerated. However, through my use of a ManualResetEvent i thought i was guarding against this.

What am i doing wrong?

  • 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-23T19:42:04+00:00Added an answer on May 23, 2026 at 7:42 pm

    (making my comment an answer, no way to format comments correctly)

    1. Thread A is enumerating | Thread B is on _CachedValuesSafe.WaitOne();
    2. Thread A finish enumerating (releasing the WaitHandle)
    3. Thread A started a new enumeration getting the lock | Thread B want to aquire lock
    4. Thread A initialize the enumeration| Thread B wait for the lock
    5. Thread A return from the GetEnumerator() call and release the lock | Thread B acquire the lock
    6. Thread A is enumerating the collection | Thread B execute Add, modifying the collection
    7. Thread A enumerate the next value and throw an exception
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to use Enumerable.ToList() in PowerShell. Apparently, to do that, I have to
I'd like to have a list that will shorten a field value if it
In .NET, both array and list have Enumerable as ancestor, so a method that
OK so I have a strongly-typed Customer Details view that takes a Customer object
I have created a universalrepository that takes the type passed to it and when
I have a search method that takes a list of ID's and I wish
I have an Expression that is used to get a list of items from
I have a repository with an Add method that takes an IEnumerable as parameter:
I have a factory that takes in IEnumerable<IPredicate> in the constructor. My factory signature
I'm trying to find a LINQ oneliner that takes a Dictionary<String,Int> and returns a

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.