BeginInvoke线程安全吗?

qingtianzhuren2 2011-10-14 05:20:23
有时线程更新UI线程,需要用到BeginInvoke,我经常这样BeginInvoke(new MethodInvoker(delegate() {}));
但有时有点怀疑线程是否安全,下面是代码:
 row2[14] = chong;
DataGridViewRow DgvRow = new DataGridViewRow();
DgvRow.CreateCells(this.dataGridViewX3, row2);
DgvRow.Tag = d; ;
this.dataGridViewX3.BeginInvoke(new MethodInvoker(delegate()
{
dataGridViewX3.Rows.Insert(0, DgvRow);
this.ribbonTabItem5.Text = "总计[" + this.dataGridViewX3.RowCount.ToString() + "]";
}));
string time = DateTime.Now.ToString("MM-dd HH:mm:ss");
this.textBox2.BeginInvoke(new MethodInvoker(delegate() { this.textBox2.Text = time + " " + d.zhuangtai + "\r\n" + this.textBox2.Text; }));


虽然绝大部分正常,但有时 this.textBox2.BeginInvoke(new MethodInvoker(delegate() { this.textBox2.Text = time + " " + d.zhuangtai + "\r\n" + this.textBox2.Text; }));这一句执行了,但前面datagridviewx3却没有填加新行?!是不是BeginInvoke不够安全还是其它原因?dataGridViewX3.BeginInvoke中同时更新 this.ribbonTabItem5.Text 是否允许?!因为也是UI线程,所以我这么写
...全文
121 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
Indifferent_Wind 2011-10-14
  • 打赏
  • 举报
回复
绝对安全。。。
hengxinyi 2011-10-14
  • 打赏
  • 举报
回复
安全!
只在此山中 2011-10-14
  • 打赏
  • 举报
回复
用同步调用委托:
this.dataGridViewX3.Invoke(new MethodInvoker(delegate()
Bullatus 2011-10-14
  • 打赏
  • 举报
回复
可以更新多个控件,因为都是在UI线程下的
mabaolin 2011-10-14
  • 打赏
  • 举报
回复
可以一起写。

110,535

社区成员

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

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

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