1 public partial class Form1 : Form 2 { 3 4 //自动重置事件类 5 //主要用到其两个方法 WaitOne() 和 Set() , 前者阻塞当前线程,后者通知阻塞线程继续往下执行 6 AutoResetEvent autoEvent = new AutoResetEvent(false); 7 8 public Form1() 9 { 10 InitializeComponent(); 11 } 12 public static bool _stop=true; 13 private void button3_Click(object sender, EventArgs e) 14 { 15 int i = 0; 16 Thread NewThread = new Thread(Run); 17 NewThread.Start(i); 18 } 19 private void Run(object i) 20 { 21 Control.CheckForIllegalCrossThreadCalls = false; 22 int j = (int)i; 23 24 while (true) 25 { 26 j++; 27 Thread.Sleep(1000); 28 label1.Text = j.ToString(); 29 label1.Refresh(); 30 if(_stop) 31 autoEvent.WaitOne(); //阻塞当前线程,等待通知以继续执行 32 } 33 34 } 35 36 private void button1_Click(object sender, EventArgs e) 37 { 38 autoEvent.Set(); //通知阻塞的线程继续执行 39 _stop = false; 40 } 41 42 private void button2_Click(object sender, EventArgs e) 43 { 44 _stop = true; 45 } 46 }