I went into the ThreadSafeEnumerator code and made the current thread sleep if the number of items in the collection was greater than 10,000. I changed the AddItem code to the following:
public void AddItem(T item)
{
while (cached.Count > 10000)
{
Thread.Sleep(100);
}
lock (cached)
{
cached.Enqueue(item);
Monitor.Pulse(cached);
}
}
I don't know if this is safe or performant, but I never found a better solution and this seemed to do the trick. A good change to this code would be to actually measure the amount of bytes 'cached' and sleep based on that, but you'd need to do some tricks to make this perform well I think.
Good luck!