Thread.Sleep(1000)
Thread.Sleep,线程挂起,当在主线程中线程挂起会导致子线程也挂起;
private Action<int> act; public Form1() { InitializeComponent(); act = new Action<int>(setValue); } private void button1_Click(object sender, EventArgs e) { Thread th = new Thread(Fun); th.Name = "thread1"; th.Start(); } private void Fun() { for (int i = 0; i < 1000000; i++) { this.label1.Invoke(act, i); } } private void setValue(int i) { label1.Text = i.ToString(); } private void button2_Click(object sender, EventArgs e) { Thread.Sleep(1000); }
button2_Click点击后,label1.Text的变化会暂停一秒。
--- 20220125
应该不是,Thread.Sleep()相当于主线程阻塞,二label1.Text变化本来就是在主线程展示的。