[转]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;
            }
        }

转自:http://www.cnblogs.com/tuyile006/archive/2006/12/28/606304.html

posted @ 2011-03-13 16:24  愤怒的熊猫  阅读(96)  评论(0编辑  收藏  举报