日期:2014-05-18 浏览次数:20947 次
private void button1_Click(object sender, EventArgs e) { textBox1.Text = "a"; textBox2.Text = "b"; Thread t1 = new Thread(new ThreadStart(Thread1)); t1.Start(); Thread t2 = new Thread(new ThreadStart(Thread2)); t2.Start(); } public void Thread1() { if (this.InvokeRequired) { this.Invoke(new Action(Thread1)); } else { int i = 0; for (i = 0; i < 100; i++) { textBox1.Text = "a" + i.ToString(); Thread.Sleep(0); //Application.DoEvents(); } } } public void Thread2() { if (this.InvokeRequired) { this.Invoke(new Action(Thread2)); } else { int i = 0; for (i = 0; i < 100; i++) { textBox2.Text = "b" + i.ToString(); Thread.Sleep(10); //Application.DoEvents(); } } }
public void Thread1() { int i = 0; for (i = 0; i < 100; i++) { this.Invoke((MethodInvoker)(() => textBox1.Text = "a" + i.ToString())); Thread.Sleep(10); } } public void Thread2() { int i = 0; for (i = 0; i < 100; i++) { this.Invoke((MethodInvoker)(() => textBox2.Text = "b" + i.ToString())); Thread.Sleep(10); } }