当前位置:编程学习 > C#/ASP.NET >>

BeginInvoke线程安全吗?

   有时线程更新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线程,所以我这么写 --------------------编程问答-------------------- 可以一起写。 --------------------编程问答-------------------- http://www.cnblogs.com/mashang/archive/2009/08/01/1536730.html参考 --------------------编程问答-------------------- 可以更新多个控件,因为都是在UI线程下的 --------------------编程问答-------------------- 用同步调用委托:
  this.dataGridViewX3.Invoke(new MethodInvoker(delegate()
--------------------编程问答-------------------- 安全! --------------------编程问答-------------------- 绝对安全。。。
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,