如何暂停一个线程
实现一个需求,在上位机项目中执行一直都PLC数据和将PLC数据写入数据的操作。当按下按钮时暂停读写操作
原理:开两个线程,每个线程执行一个死循环,在循环体内一直读PLC数据和写入数据库操作
方法一:通过控制flag来控制线程的暂停
using System; using System.Threading; using System.Threading.Tasks; using System.Windows.Forms; namespace LoadDataByTask { public partial class Form1 : Form { public Form1() { InitializeComponent(); this.button2.Text = "暂停"; } bool falg = true;//控制位 ManualResetEvent mark = new ManualResetEvent(true); private void button1_Click(object sender, EventArgs e) { falg = true; //执行读PLC数据任务 Task.Run(() => { while (true) { //第一种方法 for (int i = 0; i < 10; i++) { if (falg) { Console.WriteLine("read data from plc" + i + ":" + System.DateTime.Now.ToString()); Thread.Sleep(200); } } } }); //执行将数据写入数据库任务 Task.Run(() => { while (true) { //第一种方法 for (int i = 0; i < 10; i++) { if (falg) { Console.WriteLine("write data " + i + ":" + System.DateTime.Now.ToString()); Thread.Sleep(200); } } } }); } private void button2_Click(object sender, EventArgs e) { falg = false; } } }
方法一的缺点:
线程并不是真正的暂停,线程暂停后再启动CPU占用率很高,如下
方法2:使用ManualResetEvent进行控制线程的暂停
using System; using System.Threading; using System.Threading.Tasks; using System.Windows.Forms; namespace LoadDataByTask { public partial class Form1 : Form { public Form1() { InitializeComponent(); this.button2.Text = "暂停"; } ManualResetEvent mark = new ManualResetEvent(true); private void button1_Click(object sender, EventArgs e) { falg = true; //执行读PLC数据任务 Task.Run(() => { while (true) { //第二种方法 for (int i = 0; i < 100; i++) { int k = i; mark.WaitOne(); //Console.WriteLine("read data from plc" + k + ":" + System.DateTime.Now.ToString()); Console.WriteLine("read data from plc" + k + "当前线程:" + Thread.CurrentThread.ManagedThreadId.ToString()); Thread.Sleep(1000); } } });
//执行将数据写入数据库任务 Task.Run(() => { while (true) { //第二种方法 mark.WaitOne(); for (int i = 0; i < 100; i++) { int k = i; mark.WaitOne(); //Console.WriteLine("write data " + k + ":" + System.DateTime.Now.ToString()); Console.WriteLine("write data " + k + "@" + "当前线程:" + Thread.CurrentThread.ManagedThreadId.ToString()); Thread.Sleep(1000); } } }); } private void button2_Click(object sender, EventArgs e) { if (this.button2.Text == "暂停") { mark.Reset(); this.button2.Text = "恢复"; } else { mark.Set(); this.button2.Text = "暂停"; } } } }
第二种线程暂停的方案为最佳方案,线程暂停的时候不会引起CPU占用率的增加。
例子:使用ManualResetEvent进行控制线程暂停的另一个例子
引用:【C#】怎样做线程的[暂停/继续]操作_哔哩哔哩_bilibili,感谢UP糖君哒
实现一个PrograssBar的暂停,效果入下:
标签:System,button2,线程,暂停,using,true From: https://www.cnblogs.com/hanzq/p/16835695.html