Winform中进行线程安全的多线程操作(主线程控件,非backgroundWorker)

用这种多线程的方式也可以实现安全操作主线程的控件。

        private void Form1_Load(object sender, EventArgs e)
        {
            Thread t = new Thread(Take);
            t.IsBackground=true;
            t.Start();
        }

        private void btnGenerate_Click(object sender, EventArgs e)
        {
            list.Add(txtLog.Text);
        }

        private void Take()
        {
            while(true){
                   foreach(String s in list.ToArray()){
                       MyDeleage d = (txt) =>
                       {
                         txtCon.AppendText(txt+"\n");
                       };
                       txtCon.Invoke(d, s);
                       list.Remove(s);
                       Thread.Sleep(2000);
                   }
            }

        }

        delegate void MyDeleage(string s);

  

posted @ 2013-05-06 09:26  冰深  阅读(363)  评论(0编辑  收藏  举报