双11狂欢大促

盲盒赢 定制羽绒服

开启惊喜盲盒,抽取你的专属好礼

— 惊喜奖品抢先看 —

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

chiqp1986 chiqp1986 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
在界面上有个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)时报错。
...全文

相关推荐

评论 4
收藏 已收藏
分享
返回顶部
chiqp1986
C# 111,129 成员 642,540 内容