您的位置:首页 > 其它

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

2007-06-29 18:06 246 查看
解决办法:

创建代理
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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: