用这种多线程的方式也可以实现安全操作主线程的控件。
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);