后台线程更新DataGridView数据的一个异常更改

来源:互联网 发布:ibm服务器安装linux 编辑:程序博客网 时间:2024/06/02 09:31

 

程序中用到一个线程不停的更新主界面上的一个DATAGRIDVIEW,更新时,经常出现如下 错误:

Object reference not set to an instance of an object..
   at System.Windows.Forms.DataGridViewRow.GetErrorText(Int32 rowIndex)
   at System.Windows.Forms.DataGridViewRow.Paint(Graphics graphics, Rectangle clipBounds, Rectangle rowBounds, Int32 rowIndex, DataGridViewElementStates rowState, Boolean isFirstDisplayedRow, Boolean isLastVisibleRow)
   at System.Windows.Forms.DataGridView.PaintRows(Graphics g, Rectangle boundingRect, Rectangle clipRect, Boolean singleHorizontalBorderAdded)
   at System.Windows.Forms.DataGridView.PaintGrid(Graphics g, Rectangle gridBounds, Rectangle clipRect, Boolean singleVerticalBorderAdded, Boolean singleHorizontalBorderAdded)
   at System.Windows.Forms.DataGridView.OnPaint(PaintEventArgs e)
   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.DataGridView.WndProc(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
   at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
   at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
   at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
   at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
   at System.Windows.Forms.Application.Run(Form mainForm)
   at HWPT.TrackingStation.Program.Main() in D:/KJ297/TrackingStation/Program.cs:line 122

根据DataGridView的事件 DataError 可以捕获取是 Display 时的错误。因此基本确定时线程与主界面线程资源处理的问题。大概的假设就是:后台已经删除了一条记录,而前台主界面线程又去刷新显示实际上已经被删除的记录。从而导致异常。

那后台线程不能删除,就移到前台线程中来删除了:) 使用委托可以实现这个目的:

 

        //定义一个委托
       delegate void dlgtGenePara1(int iValue);
       
//这里用于DataGridView的删除等操作
        void DelRoute(int iRoute)
        
{
            DataView dv 
= dgvRoutes.DataSource as DataView;//dgvRoutes 为一个DataGridView 对象
            lock (dv)
            
{
                
for (int i = 0; i < dv.Count; ++i)
                
{
                    
if ((dv[i]["obj"as CRouteMoniteringMgr.CItem).route.ID == iRoute)
                    
{
                        dv[i].Delete();
                        
break;
                    }

                }

            }

            dgvRoutes.Refresh();
        }

在后台线程事如果需要进行删除更新等操作,则直接使用委托调用即可。

--------------------------------------------------------------------------------------------------------

在程序中进行这么更改后,原先的异常目前暂时不再发生,因此基本断定就是这个原因导致的。

欢迎大家对此补充。

原创粉丝点击