委托出现 从不是创建控件的线程访问它

.Net技术 码拜 9年前 (2014-12-23) 2047次浏览 0个评论

从一个窗体到另外一个窗体的委托没有问题。

然后从一个非窗体类 到 窗体 的委托时,出现错误。信息如下:

引用

线程间操作无效:  从不是创建控件 “textBox1”的线程访问它。

编写的程序代码如下

namespace thread_product_consume2
{
    public delegate void UpdataTextBox(string str);
    public class Sushi
    {
        public UpdataTextBox updata_TXT;
       ..............
    }
    class CellConsum  { .....}
    class CellProd{.....}
  }
   public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            this.textBox1.Text = null;
            Add_Text("ABC1");
            int result = 0;

            Sushi FoodSushi = new Sushi();
            FoodSushi.updata_TXT = new UpdataTextBox( Add_Text);
            //用单元初始化二个实例
            CellProd prod = new CellProd(FoodSushi, 20);
            CellConsum consum = new CellConsum(FoodSushi, 20);
        }
        //在文本框输出文字
        public void Add_Text(string str)
        {
            this.textBox1.Text +=str;
        }
  }

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明委托出现 从不是创建控件的线程访问它
喜欢 (0)
[1034331897@qq.com]
分享 (0)

文章评论已关闭!