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"); } } }