using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; using System.Windows.Forms; namespace XCZT { public partial class Form1 : Form { public Form1() { InitializeComponent(); Label.CheckForIllegalCrossThreadCalls = false; } ManualResetEvent ma = new ManualResetEvent(false); bool stop = false; //启动 private void button1_Click(object sender, EventArgs e) { Thread thread = new Thread(Runtime); stop = false; ma.Set();// 信号打开,不阻塞当前线程 thread.Start(); } /// <summary> /// 线程 /// </summary> void Runtime() { for (int i = 1; i <= 100; i++) { if (stop) { return; } ma.WaitOne();//根据是否收到信号判断是否阻塞当前线程 textBox1.AppendText("计时 :" + i + "\r\n"); Thread.Sleep(100); } } //暂停 private void button2_Click(object sender, EventArgs e) { ma.Reset();//信号关闭阻塞当前线程 textBox1.AppendText("暂停中 :\r\n"); } //继续 private void button3_Click(object sender, EventArgs e) { ma.Set();//信号打开,不阻塞当前线程 textBox1.AppendText("继续计时 :\r\n"); } //停止 private void button4_Click(object sender, EventArgs e) { stop = true; textBox1.AppendText("停止计时 \r\n"); } } }