Randomly encountered System.ArgumentOutOfRangeException when many threads change the SfDataGrid.DataSource (ObservableCollection)

I have an application that runs many underlying theads to do some works.
I decided to use sfDataGrid for showing those threads' status.
My implementation is like this:
1. I use an ObservableCollection<TaskThread> where TaskThread is a class implements INotifyPropertyChanged to save the information about the underlying threads.
2. I use ThreadPool to manage my underlying threads
3. To be able to change the limit of the active threads, I set ThreadPool.SetMaxThreads() to a huge number. Then the limit_of_the_active_threads is set to a much smaller number. I run my own logic to check and do the following things:
  • If the active_thread_count exceeds the limit, I stop creating new thread
  • Otherwise, I create [limit_of_the_active_threads  - active_thread_countnew thread
4. Each thead runs the following method:
  • private void CheckThread(object callback)
  • {
  •     var thisCheckThread = CreateNewThread(); //add a new item into the ObservableCollection<TaskThread>



  •     while (_maxThreadCount > GetUsingThreadCount())
  •     {
  •         var currentTask = DequeueTask();
  •         if (currentTask != null)
  •         {
  •             // do the task
  •         }
  •         Thread.Sleep(1000);
  •     }
  •     GC.Collect();



  •     RemoveThread(thisCheckThread.ThreadId); //remove the corresponding item in ObservableCollection<TaskThread>
  • }
Everything works like a charm, but the System.ArgumentOutOfRangeException is fired randomly. I don't know why this exception keeps coming and also don't know how to handle it (I just want to ignore it so that my application doesn't crash).
Could you help me solve this?
Thank you in advance.

5 Replies

VS Vijayarasan Sivanandham Syncfusion Team June 30, 2020 04:51 PM UTC

Hi Truong Giang,

Thank you for contacting Syncfsuion support.

Can you please share us below things?
       
        1. Stack trace of the exception details

        2. Syncfusion product version
       
It will help us to investigate further and provide appropriate solution at earlier.

Regards,
Vijayarasan S





UN Unknown replied to Vijayarasan Sivanandham June 30, 2020 10:44 PM UTC

Hi Truong Giang,

Thank you for contacting Syncfsuion support.

Can you please share us below things?
       
        1. Stack trace of the exception details

        2. Syncfusion product version
       
It will help us to investigate further and provide appropriate solution at earlier.

Regards,
Vijayarasan S




Thank you for your reply, 

1. Here is the Detail Log (Syncfusion version included) when my app crashes

See the end of this message for details on invoking 
just-in-time (JIT) debugging instead of this dialog box.

************** Exception Text **************
System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
   at System.ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument argument, ExceptionResource resource)
   at System.Collections.Generic.List`1.get_Item(Int32 index)
   at Syncfusion.Data.RecordsListBase.GetRecord(Int32 index)
   at Syncfusion.WinForms.DataGrid.RowGenerator.UpdateRow(IEnumerable`1 rows, Int32 rowIndex)
   at Syncfusion.WinForms.DataGrid.RowGenerator.EnsureRows(VisibleLinesCollection visibleRows)
   at Syncfusion.WinForms.DataGrid.GridPainter.EnsureItems(Boolean ensureColumns)
   at Syncfusion.WinForms.DataGrid.GridPainter.OnPaint(PaintEventArgs e, Size clientSize)
   at Syncfusion.WinForms.DataGrid.TableControl.OnPaint(PaintEventArgs e)
   at System.Windows.Forms.Control.PaintWithErrorHandling(PaintEventArgs e, Int16 layer)
   at System.Windows.Forms.Control.WmPaint(Message& m)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at Syncfusion.WinForms.Controls.SfScrollControl.WndProc(Message& msg)
   at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)


************** Loaded Assemblies **************
mscorlib
    Assembly Version: 4.0.0.0
    Win32 Version: 4.7.3620.0 built by: NET472REL1LAST_B
    CodeBase: file:///C:/Windows/Microsoft.NET/Framework64/v4.0.30319/mscorlib.dll
----------------------------------------
Syncfusion.Shared.Base
    Assembly Version: 18.1460.0.42
    Win32 Version: 18.1460.0.42
    CodeBase: file:///C:/Users/Administrator/Desktop/QuickCheckCamp/NewQuickCheckCamp/Syncfusion.Shared.Base.DLL
----------------------------------------
Syncfusion.SfDataGrid.WinForms
    Assembly Version: 18.1460.0.42
    Win32 Version: 18.1460.0.42
    CodeBase: file:///C:/Users/Administrator/Desktop/QuickCheckCamp/NewQuickCheckCamp/Syncfusion.SfDataGrid.WinForms.DLL
----------------------------------------
Syncfusion.Data.WinForms
    Assembly Version: 18.1460.0.42
    Win32 Version: 18.1460.0.42
    CodeBase: file:///C:/Users/Administrator/Desktop/QuickCheckCamp/NewQuickCheckCamp/Syncfusion.Data.WinForms.DLL
----------------------------------------
Syncfusion.Core.WinForms
    Assembly Version: 18.1460.0.42
    Win32 Version: 18.1460.0.42
    CodeBase: file:///C:/Users/Administrator/Desktop/QuickCheckCamp/NewQuickCheckCamp/Syncfusion.Core.WinForms.DLL
----------------------------------------
Syncfusion.SfInput.WinForms
    Assembly Version: 18.1460.0.42
    Win32 Version: 18.1460.0.42
    CodeBase: file:///C:/Users/Administrator/Desktop/QuickCheckCamp/NewQuickCheckCamp/Syncfusion.SfInput.WinForms.DLL
----------------------------------------
Syncfusion.GridCommon.WinForms
    Assembly Version: 18.1460.0.42
    Win32 Version: 18.1460.0.42
    CodeBase: file:///C:/Users/Administrator/Desktop/QuickCheckCamp/NewQuickCheckCamp/Syncfusion.GridCommon.WinForms.DLL
----------------------------------------
Syncfusion.SfListView.WinForms
    Assembly Version: 18.1460.0.42
    Win32 Version: 18.1460.0.42
    CodeBase: file:///C:/Users/Administrator/Desktop/QuickCheckCamp/NewQuickCheckCamp/Syncfusion.SfListView.WinForms.DLL
----------------------------------------
Syncfusion.DataSource.WinForms
    Assembly Version: 18.1460.0.42
    Win32 Version: 18.1460.0.42
    CodeBase: file:///C:/Users/Administrator/Desktop/QuickCheckCamp/NewQuickCheckCamp/Syncfusion.DataSource.WinForms.DLL

2. Here is the screen shot


3. I also tried to make my app "eat" the Exception like the comment below
Hi Plioi, If you want to catch errors while the grid is being initially created then you may subscribe this handler in the form''s constructor before the InitializeComponent call, as the grid is usually created in the InitializeComponent call from the forms constructor. ExceptionManager.ExceptionCatched += new ExceptionCatchedEventHandler(ExceptionManager_ExceptionCatched); Then in the handler you would condition resume or suspend CatchExceptions depending upon what you want the grid to do. private void ExceptionManager_ExceptionCatched(object sender, ExceptionCatchedEventArgs e) { // if you want the grid to rethrow the exception, call ExceptionManager.SuspendCatchExceptions(); // if you want the grid to eat the exception (ignore it), then call ExceptionManager.ResumeCatchExceptions(); } Best regards, Stanley

But nothing changes, the Exception still fired and my app still crashed. The ExceptionManager_ExceptionCatched was not even being called.


Please let me know if you want more information.



VS Vijayarasan Sivanandham Syncfusion Team July 1, 2020 04:46 PM UTC

Hi Truong Giang,

Thanks for the update.

 
We have created a new incident under your Direct trac account. We suggest you to follow up with the incident for further updates. Please log in using the below link. 

https://www.syncfusion.com/support/directtrac/incidents/283496 

Regards,
 
Vijayarasan S 



AG Anmol Gupta replied to Vijayarasan Sivanandham May 17, 2023 10:12 AM UTC

I am facing the same issue. Do you get any solution to this?




VS Vijayarasan Sivanandham Syncfusion Team May 18, 2023 02:00 PM UTC

Hi Anmol Gupta,

We suspect that the reported problem occurs due to adding and removing the records continuously to the underlying collection. Based on our control architecture, painting will be triggered while adding/removing the items to the DataSource. At some point in your application, the removal of the record is performed before completing the painting and ensuring the previously added record. In such case the record count in the view and the items count in the underlying DataSource will be mismatched. This leads to accessing the record using some invalid index which causes the ArgumentOutOfRangeException.  

However, you can resolve the reported problem by calling SfDataGrid.BeginInit() method before adding/removing an item and called SfDataGrid.EndInit() method after adding/removing an item from the underlying collection. This resolves the exception. But it will suspend/resume the DataGrid process while adding/removing the items. Refer to the below code snippet,

// Batch Updates

 

//Suspends data manipulation operations in View

SfDataGrid.BeginInit();

 

// Add, remove, and update the underlying collection.

 

//Resumes data manipulation operations and refresh the View.

SfDataGrid.EndInit();


UG Link: https://help.syncfusion.com/windowsforms/datagrid/databinding#sourcecollectionchanged

Regards,

Vijayarasan S


If this post is helpful, please consider Accepting it as the solution so that other members can locate it more quickly.


Loader.
Up arrow icon