DataGrid and multiple threads
-
I’m using the MS DataGrid on a form. Each row displays the state of an instance of a class. When I load this form it creates the collection of these classes and spawns a maintenance thread that keeps these instances updated with info scraped from a website. Each class instance has a reference to the DataTable of the DataGrid and is able to update its own row in the grid. This works fine. You can sit back and watch this grid refreshing "in the background" while manipulating other elements of the form. I ran into problems when I tried to have this thread delete a row from the grid. Intuitively this makes sense since there is probably a race condition between the form thread and the maintenance thread where the delete is taking place. I got around this problem by having the actual grid row deleted on the form thread. But I'm worried that there might be some hidden race condition waiting to happen when I do my updates. Any comments would be greatly appreciated. :) Thanks, Kevin Here is the error I get when I delete from the Update thread. ===== An unhandled exception of type 'System.IndexOutOfRangeException' occurred in system.windows.forms.dll Additional information: No value at index 2. ===== Unhandled Exception: System.IndexOutOfRangeException: No value at index 2. at System.Windows.Forms.CurrencyManager.get_Item(Int32 index) at System.Windows.Forms.DataGridRow.PaintHeader(Graphics g, Rectangle visualBounds, Boolean alignToRight, Boolean rowIsDirty) at System.Windows.Forms.DataGridRelationshipRow.PaintHeaderInside (Graphics g, Rectangle bounds, Brush backBr, Boolean alignToRight, Boolean isDirty) at System.Windows.Forms.DataGridRelationshipRow.PaintHeader(Graphics g, Rectangle bounds, Boolean alignToRight, Boolean isDirty) at System.Windows.Forms.DataGrid.PaintRows(Graphics g, Rectangle& boundingRect) at System.Windows.Forms.DataGrid.PaintGrid(Graphics g, Rectangle gridBounds) at System.Windows.Forms.DataGrid.OnPaint(PaintEventArgs pe) at System.Windows.Forms.Control.PaintWithErrorHandling(PaintEventArgs e, Int16 layer, Boolean disposeEventArgs) at System.Windows.Forms.Control.WmPaint(Message& m) at System.Windows.Forms.Control.WndProc(Message& m) at System.Windows.Forms.ControlNativeWindow.OnMessage(Message& m) at System.Windows.Forms.ControlNativeWindow.WndProc(Message& m) at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lpara