VS2005的“从不是创建控件的线程访问它”

解决办法:

创建代理
delegate void SetTextCallback(string text);

创建和启动线程
this.demoThread = 
               new Thread(new ThreadStart(this.ThreadProcUnsafe));
               this.demoThread.Start();

线程中要求改主窗体UI中的text属性
private void ThreadProcSafe()
        {
            this.SetText("This text was set safely.");
        }

调用窗体中的函数用invoke传递参数
private void SetText(string text)
        {
            if (this.textBox1.InvokeRequired)
            {   
                SetTextCallback d = new SetTextCallback(SetText);
                this.Invoke(d, new object[] { text });
            }
            else
            {
                this.textBox1.Text = text;
            }
        }

posted @ 2006-12-28 17:34  小y  阅读(3680)  评论(4编辑  收藏  举报