skybreak

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

        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);

  

分类:

技术点:

相关文章:

  • 2022-12-23
  • 2021-11-24
  • 2021-10-12
  • 2021-05-02
  • 2022-12-23
  • 2021-11-18
  • 2021-09-21
猜你喜欢
  • 2021-06-16
  • 2022-01-16
  • 2022-01-29
  • 2021-09-24
  • 2022-03-03
  • 2021-11-23
  • 2021-11-27
相关资源
相似解决方案