Hello,
I'm using the InfragisticsWPF4 libraries, version 11.1.20111.2053.
I'm getting the following exception when adding large numbers of objects to a subclass instance of ObservableCollection<T> bound to a XamDataGrid:
Message:
Specified argument was out of the range of valid values.
Parameter name: index
StackTrace:
at Infragistics.Collections.SparseArray.Insert(Int32 index, Object item)
at Infragistics.Windows.DataPresenter.RecordCollectionBase.InsertRecord(Int32 index, Record record)
at Infragistics.Windows.DataPresenter.RecordManager.InsertRecord(Int32 index, Int32 sortedIndex, DataRecord record, Boolean notifyListeners)
at Infragistics.Windows.DataPresenter.RecordManager.OnSourceCollectionAddOrRemove(NotifyCollectionChangedEventArgs e)
at Infragistics.Windows.DataPresenter.RecordManager.OnSourceCollectionChanged(Object sender, NotifyCollectionChangedEventArgs e)
at Infragistics.Windows.DataPresenter.RecordManager.ProcessChangeNotification(Object sender, Object eventArgs)
at Infragistics.Windows.DataPresenter.RecordManager.OnChangeNotification(Object sender, Object eventArgs, Boolean isReset)
at Infragistics.Windows.DataPresenter.RecordManager.System.Windows.IWeakEventListener.ReceiveWeakEvent(Type managerType, Object sender, EventArgs e)
at System.Windows.WeakEventManager.DeliverEventToList(Object sender, EventArgs args, ListenerList list)
at System.Windows.WeakEventManager.DeliverEvent(Object sender, EventArgs args)
at System.Collections.Specialized.CollectionChangedEventManager.OnCollectionChanged(Object sender, NotifyCollectionChangedEventArgs args)
Other Information:
1. My collection has a reference to the System.Windows.Threading.Dispatcher associated with the UI thread.
2. My code that adds and removes items from the collection dispatches the Add() and Remove() calls to the UI thread using Dispatcher.BeginInvoke(). DispatcherPriority is "Send"
3. My collection class overrides the CollectionChanged event so that I can track subscriber handlers.
4. My collection class overrides OnCollectionChanged. Here is where, for each subscriber handler, I call Dispatch.BeginInvoke() to execute the handler on the UI thread. DispatcherPriority is "Send". This is the site of the exception. The code that executes the handler looks like this:
uiDispatcher.BeginInvoke(handlerDelegate, DispatcherPriority.Send, sender, args);
Summary
1. I am adding and removing items to my collection on the UI thread
2. I am dispatching collectionChanged events on the UI thread
3. To my knowledge, Dispatcher.BeginInvoke() functions in a way that, for the UI thread, calls to BeginInvoke() are executed in order.
Any help getting past this issue is greatly appreciated.
Thanks,
Paul
HI Paul,
Is there anything further that I can help with regarding this question?
If so, please feel let me know.
Hi Paul,
I didn’t mean to imply that you would be removing all items at once. There are a number of ways to update the contents of a collection. The following link is helpful with the details.
http://msdn.microsoft.com/en-us/library/ms132413.aspx
But you could identify an index of the object in the collection and use
collection.RemoveAt(index); or you could use collection.Remove(n); where n is an item is the collection.
If you actually wanted to remove all of the items you would most likely use collection.Clear();
Hi Marianne,
Unfortunately, this doesn't solve my problem. The reason is that, in my real-world app, I'm not clearing my collection at a predetermined time. Therefore, I cannot remove the 0th item to shorten the list. An unknown number of items will be added and an unknown number of items will be removed. The items removed could be anywhere in the collection. The items are removed using the Remove() method which ultimately calls the RemoveAt() method internally.
I’ve been working with your sample and I see this exception being thrown (“Specified argument was out of the range of valid values. Parameter name: index”)
I believe that what is happening is not related to the threading. It seems to be an issue of trying to removeAt an index that isn’t valid. I made several modifications to your code including modifying your while statement to limit it to the trunacteAt value and moved the removeAt code to outside the while statement. One other thing was to limit the for statement to index < collection.Count – 1 and the code no longer throws any exception.
private void UpdateCollection(object threadContext){ DispatchedObservableCollection<BusinessObjectClass> collection = threadContext as DispatchedObservableCollection<BusinessObjectClass>; int i = 0; int truncateAt = 10; //int truncateAt = 1000; //while (true) while (i < truncateAt ) { //truncate the list to simulate dome deletions //if (collection.Count != 0 && collection.Count % truncateAt == 0) //{ // for (int index = 0; index < collection; index++) // { // collection.RemoveAt(index); // } // return; //} BusinessObjectClass n = new BusinessObjectClass(); n.Title = "Title" + i; n.Description = "Description"; n.Text1 = "Some Text"; n.Text2 = "Some Text"; n.Text3 = "Some Text"; n.Text4 = "Some Text"; n.Text5 = "Some Text"; n.Text6 = "Some Text"; n.Text7 = "Some Text"; n.Text8 = "Some Text"; n.Text9 = "Some Text"; n.Text10 = "Some Text"; collection.Add(n); i++; } if (collection.Count != 0 && collection.Count % truncateAt == 0){ int index = 0; do { collection.RemoveAt(0); index++; } while (index < collection.Count ); } }
private void UpdateCollection(object threadContext){ DispatchedObservableCollection<BusinessObjectClass> collection = threadContext as DispatchedObservableCollection<BusinessObjectClass>; int i = 0; int truncateAt = 10; //int truncateAt = 1000;
//while (true) while (i < truncateAt ) { //truncate the list to simulate dome deletions //if (collection.Count != 0 && collection.Count % truncateAt == 0) //{ // for (int index = 0; index < collection; index++) // { // collection.RemoveAt(index); // } // return; //}
BusinessObjectClass n = new BusinessObjectClass(); n.Title = "Title" + i; n.Description = "Description"; n.Text1 = "Some Text"; n.Text2 = "Some Text"; n.Text3 = "Some Text"; n.Text4 = "Some Text"; n.Text5 = "Some Text"; n.Text6 = "Some Text"; n.Text7 = "Some Text"; n.Text8 = "Some Text"; n.Text9 = "Some Text"; n.Text10 = "Some Text"; collection.Add(n); i++; }
if (collection.Count != 0 && collection.Count % truncateAt == 0){ int index = 0; do { collection.RemoveAt(0); index++; } while (index < collection.Count ); } }
The code as I have it will remove all of the items in the collection.
Any updates in this regard? I seem to be having the same issue which results in a Null Reference Exception. Am trying to delete consecutive records which results in the error.
Srikanth