WPF: Question regarding an ArgumentException I sometimes get when binding to a IEnumerable with a growing collection

38 views
Skip to first unread message

Sebastian Huland

unread,
Sep 9, 2015, 10:56:50 AM9/9/15
to Assisticant
Hi Michael,

(This is for WPF using latest Assisticant)

I seem to have run into an issue where a (rapidly) growing collection that is bound via the ViewModel to a grid can run into a "Destination array was not long enough. Check destIndex and length, and the array's lower bounds. ArgumentException" when it grows over a certain amount.

This doesn't always happen, but there's a good chance it does. The Collection at the time of the Exception has just over 1100 members.

CallStack is as follows:

   at System.Array.Copy(Array sourceArray, Int32 sourceIndex, Array destinationArray, Int32 destinationIndex, Int32 length, Boolean reliable)
   at System.Collections.Generic.List`1.CopyTo(T[] array, Int32 arrayIndex)
   at Assisticant.Collections.ObservableList`1.CopyTo(T[] array, Int32 arrayIndex)
   at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
   at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)

Any ideas?




Michael L Perry

unread,
Sep 9, 2015, 11:15:57 AM9/9/15
to Assisticant
This sounds like a threading issue. Perhaps you are calling a method that returns an IEnumerable from a lock? With the delayed execution of ToList, it would be outside of the lock by the time it copies the array. So it allocates the array for the new list based on one size, and then copies it after another thread has added to it.

Sebastian Huland

unread,
Sep 9, 2015, 11:34:19 AM9/9/15
to Assisticant
It was indeed... thanks very much :)

Michael L Perry

unread,
Sep 9, 2015, 12:10:29 PM9/9/15
to Assisticant
I can't tell you how many times I've done that myself. :)
Reply all
Reply to author
Forward
0 new messages