博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
c#后台线程更新界面
阅读量:6416 次
发布时间:2019-06-23

本文共 11535 字,大约阅读时间需要 38 分钟。

参考文章《》

 

主窗口

public frmMain()        {            InitializeComponent();        }        ///         /// 主窗口加载事件        ///         ///         ///         private void frmMain_Load(object sender, EventArgs e)        {            try            {                //设置默认状态                mc_MachineStatusHander = new Common.MachineStatusHandler();                                mc_MachineStatusHander.mc_MachineProduceStatusHandler.UpdateMachineSignalDelegate += UpdateMachineSignalStatus;//后台线程中指定委托为更新界面方法                mc_MachineStatusHander.ChangeStatus("Online", "运行", "WesChen", "001A");                SettingLight();            }            catch (Exception ex)            {                ShowErrorMessage(ex.Message, "系统初始化");                this.Close();            }        }

 

界面更新主方法

///         /// 获取机器信号,并更新界面        ///         /// 机器信号        delegate void UpdateMachineProduceSignalDelegate(Common.MachineProduceStatusHandler.SignalType singnal);        private void UpdateMachineSignalStatus(Common.MachineProduceStatusHandler.SignalType signal)        {            if (InvokeRequired)            {                this.Invoke(new UpdateMachineProduceSignalDelegate(delegate (Common.MachineProduceStatusHandler.SignalType s)                {                    if (signal.ToString().IndexOf("X01") != -1)                    {                        btn_SignalX01.BackColor= System.Drawing.Color.FromArgb(255, 255, 255);                        btn_SignalX01.ForeColor = System.Drawing.Color.FromArgb(0, 0, 0);                    }                    else                    {                        btn_SignalX01.BackColor = System.Drawing.Color.FromArgb(38, 45, 58);                        btn_SignalX01.ForeColor = System.Drawing.Color.FromArgb(255, 255, 255);                    }                                        lab_ProductCount.Text = "累计生产数量:" + mc_MachineStatusHander.mc_MachineProduceStatusHandler.ProductCount;                    lab_LastLifeCycleTime.Text = "最后一次生产用时:" + Common.CommonFunction.FormatMilliseconds(mc_MachineStatusHander.mc_MachineProduceStatusHandler.LastProductUseMilliseconds);                }), signal);            }            else            {                if (signal.ToString().IndexOf("X01") != -1)                {                    btn_SignalX01.BackColor = System.Drawing.Color.FromArgb(255, 255, 255);                    btn_SignalX01.ForeColor = System.Drawing.Color.FromArgb(0, 0, 0);                }                else                {                    btn_SignalX01.BackColor = System.Drawing.Color.FromArgb(38, 45, 58);                    btn_SignalX01.ForeColor = System.Drawing.Color.FromArgb(255, 255, 255);                }                               lab_ProductCount.Text = "累计生产数量:" + mc_MachineStatusHander.mc_MachineProduceStatusHandler.ProductCount;                lab_LastLifeCycleTime.Text = "最后一次生产用时:" + Common.CommonFunction.FormatMilliseconds(mc_MachineStatusHander.mc_MachineProduceStatusHandler.LastProductUseMilliseconds);            }        }

后台线程

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Text.RegularExpressions;using System.Diagnostics;namespace MES_MonitoringClient.Common{    ///     /// 机器生产状态    ///     public class MachineProduceStatusHandler    {        ///         /// 信号类型        ///         public enum SignalType        {            Unknow,            X01,            X02,            X03,            X01_X02,            X01_X03,            X02_X03,            X01_X02_X03        }        ///         /// 回复信号前缀        ///         private static string singnalDefaultStart = Common.ConfigFileHandler.GetAppConfig("GetSerialPortDataDefaultSignal_StartPrefix");        ///         /// 回复信号后缀        ///         private static string singnalDefaultEnd = Common.ConfigFileHandler.GetAppConfig("GetSerialPortDataDefaultSignal_EndPrefix");        /*-------------------------------------------------------------------------------------*/        ///         /// 产品生命周期(计算时间)        ///         //private List
_MachineProcedureListForTime=null; ///
/// 产品生命周期(计算次数) /// private List
_MachineProcedureListForCount=null; ///
/// 产品周期计数(生产数量) /// public int ProductCount = 0; ///
/// 空产品周期计数(不完整[空啤]生产数量) /// public int ProductErrorCount = 0; ///
/// 单次产品周期秒数 /// public long LastProductUseMilliseconds = 0; ///
/// /// public Nullable
LastX03SignalGetTime = null; ///
/// 上一个信号 /// public SignalType LastSignal; ///
/// 更新机器信号后更新界面的方法与委托 /// ///
public delegate void UpdateMachineSignal(SignalType signalType); public UpdateMachineSignal UpdateMachineSignalDelegate; /*-------------------------------------------------------------------------------------*/ ///
/// 构造函数,处理初始化的参数 /// public MachineProduceStatusHandler() { //产品生命周期(计算时间) //_MachineProcedureListForTime = new List
(); //产品生命周期(计算次数) _MachineProcedureListForCount = new List
(); } /*-------------------------------------------------------------------------------------*/ ///
/// 更新信号方法 /// ///
新信号 public void ChangeSignal(string newSingnal) { string convertSingnalString = ConvertSingnalString(newSingnal); //判断是正常的信号 if (!string.IsNullOrEmpty(convertSingnalString)) { //判断X信号 SignalType convertSingnalStatusType = ConvertSingnalStatus(convertSingnalString); if (convertSingnalStatusType != LastSignal) { #region 与上一次信号不同 if (convertSingnalStatusType == SignalType.X03) { #region 自动信号(区分上一个信号) _MachineProcedureListForCount.Add(new MachineProcedure() { ProcedureID = convertSingnalString, ProcedureCode = convertSingnalStatusType.ToString(), ProcedureName = "自动", }); if (LastSignal == SignalType.X01_X03) { //结束产品周期并计时 if (LastX03SignalGetTime.HasValue) { LastProductUseMilliseconds = (System.DateTime.Now - LastX03SignalGetTime.Value).Milliseconds; } LastX03SignalGetTime = System.DateTime.Now; } else if (LastSignal == SignalType.X02_X03) { //必须包含完整的生命周期才计数 if (CheckHaveRealProduceProcess(_MachineProcedureListForCount)) { //计数 ProductCount++; _MachineProcedureListForCount.Clear(); _MachineProcedureListForCount.Add(new MachineProcedure() { ProcedureID = convertSingnalString, ProcedureCode = convertSingnalStatusType.ToString(), ProcedureName = "自动", }); } } #endregion } else if (convertSingnalStatusType == SignalType.X01_X03 || convertSingnalStatusType == SignalType.X02_X03) { #region 开模完成==射胶完成(不区分上一个信号) //产品生命周期(计算数量) if (_MachineProcedureListForCount != null && _MachineProcedureListForCount.Count > 0) { //信号 string procedureNameString = string.Empty; if (convertSingnalStatusType == SignalType.X01_X03) procedureNameString = "开模完成"; else if (convertSingnalStatusType == SignalType.X02_X03) procedureNameString = "自动射胶"; _MachineProcedureListForCount.Add(new MachineProcedure() { ProcedureID = convertSingnalString, ProcedureCode = convertSingnalStatusType.ToString(), ProcedureName = procedureNameString, }); } #endregion } #endregion //上一个信号 LastSignal = convertSingnalStatusType; //根据信号更新界面 UpdateMachineSignalDelegate(LastSignal); } } } ///
/// 匹配信号是否正常 /// 并返回信号中的模式数字 /// ///
原信号 ///
private string ConvertSingnalString(string inputSingnal) { Regex regex = new Regex("^" + singnalDefaultStart + "[a-fA-F0-9]{4}" + singnalDefaultEnd + "$"); Match match = regex.Match(inputSingnal); if (match.Success) { Regex regexMiddle = new Regex("(?<=(" + singnalDefaultStart + "))[.\\s\\S]*?(?=(" + singnalDefaultEnd + "))", RegexOptions.Multiline | RegexOptions.Singleline); Match matchMiddle = regexMiddle.Match(inputSingnal); return matchMiddle.Value; } else { return null; } } ///
/// 模式数字转换为X[]信号 /// ///
模式数字[0800,0400,0200,0C00,0A00,0600,0E00等模式数字] ///
private SignalType ConvertSingnalStatus(string inputSingnal) { if (inputSingnal == "0800") return SignalType.X01; //开模终止信号 else if (inputSingnal == "0400") return SignalType.X02;//射胶信号 else if (inputSingnal == "0200") return SignalType.X03;//自动运行模式信号 else if (inputSingnal == "0C00") return SignalType.X01_X02; else if (inputSingnal == "0A00") return SignalType.X01_X03; else if (inputSingnal == "0600") return SignalType.X02_X03; else if (inputSingnal == "0E00") return SignalType.X01_X02_X03; else return SignalType.Unknow; } ///
/// 判断是否是真实的生产流程 /// ///
///
private bool CheckHaveRealProduceProcess(List
oldMachineProcedureList) { bool resultFlag = false; bool isX01_X03 = false; bool isX02_X03 = false; bool isX03 = false; //判断是否有完整的信号 foreach (var processItem in oldMachineProcedureList) { if (processItem.ProcedureCode == SignalType.X01_X03.ToString()) isX01_X03 = true; if (processItem.ProcedureCode == SignalType.X02_X03.ToString()) isX02_X03 = true; if (processItem.ProcedureCode == SignalType.X03.ToString()) isX03 = true; } //完整的信号则算正常生产流程 if (isX01_X03 && isX02_X03 && isX03) resultFlag = true; return resultFlag; } }}

 

 以上方法是可以正常从类内部更新winform界面的

 

转载于:https://www.cnblogs.com/weschen/p/10183685.html

你可能感兴趣的文章
6. datasource - mysql【从零开始学Spring Boot】
查看>>
编写病毒程序取款700余万,华夏银行一技术处长被捕受审
查看>>
阿里成立达摩院做基础科研 这是一家被电商掩盖的科技公司
查看>>
iPhone X降价跌破天际,国内网友:不在乎!
查看>>
商贩被保安打死?警方:初步认定系其自身疾病所致
查看>>
澳洲网:中国留学生成“香饽饽” 受澳顶尖高校青睐
查看>>
鄱阳湖上的“高空杂技人”
查看>>
甘肃宕昌中药材“入方”饲料 “药香鸡”山外飘香助脱贫
查看>>
郑杨:上交所设立科创板工作正稳步推进 “沪伦通”年内启动
查看>>
苏索轰世界波 米兰2:0热那亚重返意甲前四
查看>>
中瑞创新产业中心在杭揭牌 千万补助推动科技创新交流
查看>>
辽宁经济走出最困难时期 GDP增速稳中有进
查看>>
程序员牛人专访0012期|陪伴是对开发最长情的信任
查看>>
芝加哥略影 邂逅芝加哥!
查看>>
体素科技:2018年,算法驱动下的医学影像分析进展
查看>>
算法:什么是LRU算法?
查看>>
Vue 折腾记 - (8) 写一个挺靠谱的多地区选择组件
查看>>
VS Code折腾记 - (3) 多图解VSCode基础功能
查看>>
flex实现左右布局中按钮溢出隐藏效果
查看>>
Redux 高级 -- 源码分析
查看>>