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

请问怎么实现多线程中的数据绑定

2012-12-17 
请教如何实现多线程中的数据绑定?测试代码:C# codepublic partial class Form1 : Form{public class Test

请教如何实现多线程中的数据绑定?
测试代码:

C# code
public partial class Form1 : Form    {        public class Test : System.ComponentModel.INotifyPropertyChanged        {            private int m_Count = 0;            public int Count            {                get { return m_Count; }                set                {                    m_Count = value;                    NotifyPropertyChanged("Count");                }            }            public void Begin()            {                Thread thread = new Thread(Counter);                thread.Start();            }            private void Counter()            {                for (int i = 0; i < 1000; i++)                {                    Count = i;                    System.Threading.Thread.Sleep(100);                }            }            public void NotifyPropertyChanged(string info)            {                if (PropertyChanged != null)                {                    PropertyChanged(this, new PropertyChangedEventArgs(info));                }            }            #region INotifyPropertyChanged 成员            public event PropertyChangedEventHandler PropertyChanged;            #endregion        }        public Form1()        {            InitializeComponent();            Binding binding = new Binding("Text", t, "Count");            label1.DataBindings.Add(binding);        }        Test t = new Test();        private void button1_Click(object sender, EventArgs e)        {            t.Begin();        }    }


代码当然是报错:线程间操作无效: 从不是创建控件“label1”的线程访问它。

如果不用绑定的方式的话可以Invoke(xxxxx,xxxx)委托来修改控件的属性,但绑定时应该怎么委托呢???请教大伙。

热点排行