【求助】this.Invoke()报错问题

chiqp1986 2011-11-16 11:40:51
在界面上有个DataGridView,先增加一空白行,让用户输入数据,当用户确认输入(即离开当前行),判断当前行是否为空行,如果则删除。主要处理代码如下(代码从网上找的,原理基本明白,但为什么这么实现仍不明了)。


private int[] delIndex = null;//待删行号的数组
private bool delFlag = true;//是否要不间断尝试删除数组中的行
private bool deling = false;//是否有线程正在执行删除操作的锁变量

private void PushDelIndex(int _index)
{

if (delIndex == null)
{
delIndex = new int[] { _index };
}
else
{
Array.Resize(ref delIndex, delIndex.Length + 1);//展示如何扩充数组
delIndex[delIndex.Length - 1] = _index;
}

}

void delRowThread()
{
delFlag = true;
while (delFlag)
{
if (delIndex != null)
{
if (deling == false)
{
delRow();
}
}
Thread.Sleep(100);
}
}

private delegate void delRowCallBack();
void delRow()
{

if (this.InvokeRequired)
{
deling = true;
delRowCallBack o = new delRowCallBack(delRow);
this.Invoke(o);
}
else
{
for (int i = 0; i < delIndex.Length; i++)
{
dataGridView.Rows.RemoveAt(delIndex[i]);
}
delFlag = false;
deling = false;
delIndex = null;
}

}

private void DataGridView_RowLeave(object sender, DataGridViewCellEventArgs e)
{
DataGridView dataGridView = (DataGridView)sender;
bool isNullRow = true;
for (int columnIndex = 0; columnIndex < dataGridView.Columns.Count-1; columnIndex++)
{
isNullRow = isNullRow && (dataGridView.CurrentRow.Cells[columnIndex].Value == null);
}
if (isNullRow)
{
PushDelIndex(dataGridView.CurrentRow.Index);
Thread delTh = new Thread(new ThreadStart(delRowThread));
delTh.Start();
}
}



问题是:当DataGridView没失去焦点时,程序运行正常,但当DataGridView失去焦点时,程序运行到this.Invoke(o)时报错。
...全文
356 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
阿非 2011-11-16
  • 打赏
  • 举报
回复
那你在调用的位置判断相关引用类型是否为null
chiqp1986 2011-11-16
  • 打赏
  • 举报
回复
忘了将报错信息附上了。。。
报错信息如下:

未处理 System.NullReferenceException
Message=未将对象引用设置到对象的实例。
Source=System.Windows.Forms
StackTrace:
在 System.Windows.Forms.Control.MarshaledInvoke(Control caller, Delegate method, Object[] args, Boolean synchronous)
在 System.Windows.Forms.Control.Invoke(Delegate method, Object[] args)
在 System.Windows.Forms.Control.Invoke(Delegate method)
在 KeFuService_WinForm_CSharp.ProductInfoSettingForm.delRow() 位置……
在 KeFuService_WinForm_CSharp.ProductInfoSettingForm.delRowThread() 位置 ……
在 System.Threading.ThreadHelper.ThreadStart_Context(Object state)
在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
在 System.Threading.ThreadHelper.ThreadStart()
InnerException:
阿非 2011-11-16
  • 打赏
  • 举报
回复
报什么错
chiqp1986 2011-11-16
  • 打赏
  • 举报
回复
谢谢楼上的热心
问题我找到了
是由于有其它事件中将dataGridView修改了

110,561

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧