首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

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

2012-02-06 
VS2005的“从不是创建控件的线程访问它”新手,请教VS2005的“从不是创建控件的线程访问它”,是什么原因?[解决

VS2005的“从不是创建控件的线程访问它”
新手,请教VS2005的“从不是创建控件的线程访问它”,是什么原因?

[解决办法]
估计你是用了多线程.在辅助线程上去更新主线程上的控件.应该使用委托
应该这样:

C# code
delegate void DisplayStringDelegate(string str);private void AppendString(string str){if(this.Label1.InvokeRequird){DisplayStringDelegate dd=new DisplayStringDelegate(AppendString);this.Label1.Invoked(dd "abcdefg");}else{this.Label.Text+=str;}}
[解决办法]
C# code
private delegate void Handler(); //可带参数..private void Accessor() //注意: 参数列表, 要与 Handler 对应..{    if (this.InvokeRequired)    {        this.Invoke(new Handler(this.Accessor)); //如有参数, 在此传入..    }    else    {        lock (this)        {            //TODO: 在这里访问或处理..        }    }}private void Run(){    Thread thread = new Thread(new ThreadStart(this.Accessor));    thread.Name = "访问控件";    thread.IsBackground = true;    thread.Start();}//// 说明: 调用 Run 即可..// 

热点排行