当前位置: 首页 > news >正文

上位机开发常用技术 C# Task 线程 开始,暂停,继续,停止

        上位机开发中一定会用到的技术就是 设备的线程开始运行执行生产流程,在生产过程中会有要打开安全门或暂停设备动作,人为去排除设备小问题的时就要用到暂停功能,问题排除后设备继续运行,生产完成后设备停止。 这些操作是上位机开发中必须要实现的功能。下面是一个简单的示例。

 1.界面

2.代码

2.1主要用到的对象

//线程源
private CancellationTokenSource cts = new CancellationTokenSource();
//手动停止事件对象
private ManualResetEvent resetEvent = new ManualResetEvent(true);
//线程
private Task task;

2.2 开始

/// <summary>
/// 开 始
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnStart_Click(object sender, EventArgs e)
{if (cts.IsCancellationRequested){cts = new CancellationTokenSource();}resetEvent.Set();task = Task.Factory.StartNew(() =>{int count = 1;Color clrInfo = Color.Blue;while (!cts.IsCancellationRequested){resetEvent.WaitOne();//阻止当前线程var strInfo = "运行日志[" + count + "]";AddListViewThread(null, strInfo, clrInfo);count++;Thread.Sleep(1000);}});
}

2.3 暂 停

/// <summary>
/// 暂 停
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnPause_Click(object sender, EventArgs e)
{resetEvent.Reset();
}

2.4继 续

/// <summary>
/// 继 续
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnContinue_Click(object sender, EventArgs e)
{resetEvent.Set();
}

2.5停 止

/// <summary>
/// 停 止
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnStop_Click(object sender, EventArgs e)
{cts.Cancel();
}

全部代码

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Dynamic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;namespace TaskWindowsFormsApp
{public partial class Form1 : Form{public Form1(){InitializeComponent();}delegate void AddListViewCallback(string strTime, string strContent, Color textColor);//线程源private CancellationTokenSource cts = new CancellationTokenSource();//手动停止事件对象private ManualResetEvent resetEvent = new ManualResetEvent(true);//线程private Task task;/// <summary>/// 开 始/// </summary>/// <param name="sender"></param>/// <param name="e"></param>private void btnStart_Click(object sender, EventArgs e){if (cts.IsCancellationRequested){cts = new CancellationTokenSource();}resetEvent.Set();task = Task.Factory.StartNew(() =>{int count = 1;Color clrInfo = Color.Blue;while (!cts.IsCancellationRequested){resetEvent.WaitOne();//阻止当前线程var strInfo = "运行日志[" + count + "]";AddListViewThread(null, strInfo, clrInfo);count++;Thread.Sleep(1000);}});}/// <summary>/// 暂 停/// </summary>/// <param name="sender"></param>/// <param name="e"></param>private void btnPause_Click(object sender, EventArgs e){resetEvent.Reset();}/// <summary>/// 继 续/// </summary>/// <param name="sender"></param>/// <param name="e"></param>private void btnContinue_Click(object sender, EventArgs e){resetEvent.Set();}/// <summary>/// 显示(添加)日志/// </summary>/// <param name="strTime">时间</param>/// <param name="strContent">内裤</param>/// <param name="textColor">颜色</param>public void AddListViewThread(string strTime, string strContent, Color textColor){if (this.IsDisposed){return;}if (this.listViewWorkLogs.InvokeRequired){//获取一个值,该值指示调用方在对控件进行方法调用时是否必须调用 Invoke 方法,因为调用方位于创建控件所在的线程以外的线程中AddListViewCallback d = new AddListViewCallback(AddListViewThread);this.Invoke(d, new object[] { strTime, strContent, textColor });}else{AddContent2ListView(strTime, strContent, textColor);}}/// <summary>/// 显示(添加)日志/// </summary>/// <param name="strTime">时间</param>/// <param name="strContent">内裤</param>/// <param name="textColor">颜色</param>public void AddContent2ListView(string strTime, string strContent, Color textColor){try{if (strTime == null){strTime = string.Format("{0}:{1}:{2}", DateTime.Now.Hour, DateTime.Now.Minute, DateTime.Now.Second);}int nCount = listViewWorkLogs.Items.Count;if (nCount > 600){for (int i = nCount - 1; i > 100; i--){listViewWorkLogs.Items.RemoveAt(i);}}}catch{int nCount = listViewWorkLogs.Items.Count;if (nCount > 600){listViewWorkLogs.Clear();}}ListViewItem lvItem = new ListViewItem();lvItem.ForeColor = textColor;lvItem.Text = strTime;lvItem.StateImageIndex = 0;lvItem.SubItems.Add(strContent);this.listViewWorkLogs.Items.Insert(0, lvItem);}/// <summary>/// 停 止/// </summary>/// <param name="sender"></param>/// <param name="e"></param>private void btnStop_Click(object sender, EventArgs e){cts.Cancel();}}
}

http://www.lryc.cn/news/464421.html

相关文章:

  • MySQL 密码忘记了怎么办?
  • Java中常见的自带数据结构类
  • 数据结构——链表,哈希表
  • 如何使用Python对Excel、CSV文件完成数据清洗与预处理?
  • 第8篇:网络安全基础
  • Flutter 中的 PopScope 小部件:全面指南
  • 视频剪辑的未来
  • 通过PHP与API的结合,开启电商数据集成的新篇章
  • 使用 CDN 后 Apache 的日志记录客户真实 IP
  • ORACLE 19C安装 RAC报错
  • 省心英语 3.9.9| 资源最全面的英语学习App
  • ruoyi框架动态切换数据库
  • iba Data Export 导出面板选项
  • 过滤器Filter的介绍和使用
  • JMeter之mqtt-jmeter 插件介绍
  • Nacos2.3.2在ubuntu中的部署
  • Xilinx远程固件升级(一)——QuickBoot方案
  • O(1)调度算法与CFS
  • SpringBoot——静态资源访问的四种方式
  • WPF中的Style如何使用
  • 数据分析案例-欺诈性电子商务交易数据集可视化分析
  • java互联网医院智能导诊系统源码,Uniapp前端开发框架,支持一次编写,多端运行
  • 公交线路查询web管理系统||公交线路查询|基于SprinBoot+vue公交线路查询系统(源码+数据库+文档)
  • AI对于智能网联汽车发展路径的演化的助力
  • linux java17 - linux环境 centos7卸载java8安装java17
  • 高中数学:立体几何-外接球的外心法
  • 【Python-AI篇】人工智能python基础-计算机组成原理
  • Java Exercise
  • 滚雪球学Redis[9.1讲]:Redis的常见问题与最佳实践
  • python获取当前鼠标位置的RGB值