日期:2014-05-17  浏览次数:20693 次

线程代码,我哪儿错了?新手求指点。。
    
public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            ThreadStart threadStart = new ThreadStart(AddItem);
            Thread thread = new Thread(threadStart); //声明一个线程
            thread.Start();
        }
        private void AddItem()
        {
            for (int index = 0; index < 10000; index++)
            {
                //this.listBox1.Items.Add(string.Format("Item {0}", index));
                this.listBox1.Items.Add(string.Format("item {0}", index));
            }

        }

        private void button2_Click(object sender, EventArgs e)
        {
            MessageBox.Show(string.Format("listBox中一共有{0}项", this.listBox1.Items.Count));
        }
    }
}

------解决方案--------------------
线程里不要直接访问主界面组件 this.listBox1
http://www.cnblogs.com/mokey/articles/2095457.html
------解决方案--------------------
楼主看书不认真,肯定是点击了调试下的启动调试,而不是按开始执行来执行的程序,当然错了。。