wired exception from gridview possibly

5 posts, 0 answers
  1. hua
    hua avatar
    2 posts
    Member since:
    Apr 2015

    Posted 30 Apr 2016 Link to this post

    Hi Guys,

    My application uses gridview to hold latest 5000 records to be displayed, if new records is coming (per second), it will be added to index 0, and the oldest will be removed from ObservableCollection. Below are the sample codes:

    public ObservableCollection<EventHistory> ColDataSource {get;set;}

    private void Update(OnEventReportInput input)
    {                   
                    var history = new EventHistory();

                    ColDataSource.Insert(0, history);
                    if (ColDataSource.Count > 5000)
                    {                    
                        ColDataSource.RemoveAt(ColDataSource.Count - 1);
                    }
    }

    in most times, it works fine as my expected, but some times(randomly), my application is crashed caused by below excetion:

     

    2016-04-29 09:41:11,270 [1] ERROR Error - Application crashed | [InvalidOperationException] System.InvalidOperationException: Collection was modified; enumeration operation may not execute.
       at System.ThrowHelper.ThrowInvalidOperationException(ExceptionResource resource)
       at System.Collections.Generic.List`1.Enumerator.MoveNextRare()
       at System.Collections.Generic.List`1.Enumerator.MoveNext()
       at Telerik.Windows.Data.KeyedCollection.RefreshKeyDictionary()
       at Telerik.Windows.Data.KeyedCollection.IndexOf(Object value)
       at Telerik.Windows.Data.QueryableCollectionView.InternalIndexOf(Object item)
       at Telerik.Windows.Data.QueryableCollectionView.IndexOf(Object item)
       at Telerik.Windows.Data.Selection.ItemSelectionHandler.ScheduleForDeselectionAllNonExistentItems()
       at Telerik.Windows.Data.Selection.ItemSelectionHandler.DeselectAllNonExistentItems()
       at Telerik.Windows.Data.Selection.ItemSelectionHandler.HandleItemsReset()
       at Telerik.Windows.Data.Selection.ItemSelectionHandler.HandleItemsChanged(NotifyCollectionChangedEventArgs itemsChangedArguments)
       at Telerik.Windows.Controls.GridView.Selection.CompositeSelectionHandler.OnOwnerItemsCollectionChanged(Object sender, NotifyCollectionChangedEventArgs e)
       at Telerik.Windows.Controls.GridView.Selection.CompositeSelectionHandler.<>c__DisplayClassa.<OnOwnerItemsCollectionChangedDispatch>b__8()
       at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
       at System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Int32 numArgs, Delegate catchHandler)

    2016-04-29 09:41:11,398 [1] ERROR Error - Application crashed | [InvalidOperationException] System.InvalidOperationException: Collection was modified; enumeration operation may not execute.
       at System.ThrowHelper.ThrowInvalidOperationException(ExceptionResource resource)
       at System.Collections.Generic.List`1.Enumerator.MoveNextRare()
       at System.Collections.Generic.List`1.Enumerator.MoveNext()
       at Telerik.Windows.Data.KeyedCollection.RefreshKeyDictionary()
       at Telerik.Windows.Data.KeyedCollection.IndexOf(Object value)
       at Telerik.Windows.Data.QueryableCollectionView.InternalIndexOf(Object item)
       at Telerik.Windows.Data.QueryableCollectionView.IndexOf(Object item)
       at Telerik.Windows.Data.Selection.ItemSelectionHandler.ScheduleForDeselectionAllNonExistentItems()
       at Telerik.Windows.Data.Selection.ItemSelectionHandler.DeselectAllNonExistentItems()
       at Telerik.Windows.Data.Selection.ItemSelectionHandler.HandleItemsReset()
       at Telerik.Windows.Data.Selection.ItemSelectionHandler.HandleItemsChanged(NotifyCollectionChangedEventArgs itemsChangedArguments)
       at Telerik.Windows.Controls.GridView.Selection.CompositeSelectionHandler.OnOwnerItemsCollectionChanged(Object sender, NotifyCollectionChangedEventArgs e)
       at Telerik.Windows.Controls.GridView.Selection.CompositeSelectionHandler.<>c__DisplayClassa.<OnOwnerItemsCollectionChangedDispatch>b__8()
       at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
       at System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Int32 numArgs, Delegate catchHandler)
       at System.Windows.Threading.DispatcherOperation.InvokeImpl()
       at System.Windows.Threading.DispatcherOperation.InvokeInSecurityContext(Object state)
       at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at MS.Internal.CulturePreservingExecutionContext.Run(CulturePreservingExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Windows.Threading.DispatcherOperation.Invoke()
       at System.Windows.Threading.Dispatcher.ProcessQueue()
       at System.Windows.Threading.Dispatcher.WndProcHook(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
       at MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
       at MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o)
       at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
       at System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Int32 numArgs, Delegate catchHandler)
       at System.Windows.Threading.Dispatcher.LegacyInvokeImpl(DispatcherPriority priority, TimeSpan timeout, Delegate method, Object args, Int32 numArgs)
       at MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam)
       at MS.Win32.UnsafeNativeMethods.DispatchMessage(MSG& msg)
       at System.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrame frame)
       at System.Windows.Threading.Dispatcher.PushFrame(DispatcherFrame frame)
       at System.Windows.Application.RunDispatcher(Object ignore)
       at System.Windows.Application.RunInternal(Window window)
       at System.Windows.Application.Run(Window window)
       at System.Windows.Application.Run()
       at SevenSwords.Gui.App.Main()

     

    This crash occurs randomly per 3 or 4 hours, the only clue I could find from log is gridview,  hope can get help from your guys!  

    Thanks!

     

     

  2. hua
    hua avatar
    2 posts
    Member since:
    Apr 2015

    Posted 01 May 2016 Link to this post

    Anyone can give me any clue? thanks!
  3. Maya
    Admin
    Maya avatar
    4062 posts

    Posted 02 May 2016 Link to this post

    Hi Hua,

    What I could suggest based on the StackTrace is one of the following:
    1. Verify whether the item that is to be removed is selected or not and try deselecting it explicitly.
    2. Insert the items in another thread - in a dispatcher or through a background worker. 

    Please give those approaches a try and let me know how it went. 


    Regards,
    Maya
    Telerik
    Do you need help with upgrading your AJAX, WPF or WinForms project? Check the Telerik API Analyzer and share your thoughts.
  4. Alex
    Alex avatar
    1 posts
    Member since:
    Apr 2017

    Posted 24 Apr in reply to Maya Link to this post

    Hi Maya,

     

    There is same issue with my code, and also I am trying to use RadGridView for real time updates. What is the reason for the crash? I would like to clarify this as it may be better to modify other parts of code before implementing any of your proposals. Many thanks, Alex

  5. Dilyan Traykov
    Admin
    Dilyan Traykov avatar
    566 posts

    Posted 27 Apr Link to this post

    Hello Alex,

    The reason for this exception is that the source collection which is being iterated over by RadGridView's selection mechanism is sometimes modified (an item is removed) whilst still being iterated.

    This is the reason my colleague has suggested these two approaches in order to deal with the issue.

    Do let me know if any of these approaches work for you or any other questions or concerns arise.

    Regards,
    Dilyan Traykov
    Telerik by Progress
    Want to extend the target reach of your WPF applications, leveraging iOS, Android, and UWP? Try UI for Xamarin, a suite of polished and feature-rich components for the Xamarin framework, which allow you to write beautiful native mobile apps using a single shared C# codebase.
Back to Top