using System; using System.Collections.Generic; using System.Linq; using System.Text; using Mesnac.Action.Base; using System.Configuration; namespace Mesnac.Action.Feeding.Qingquan.Sys { /// /// 定时器运行服务类 /// public class TimerRunService { #region 字段定义 private System.Timers.Timer _timer = null; private int _cnt = 0; //计数器 #endregion #region 单例实现 private static TimerRunService _instance = null; private TimerRunService() { } public static TimerRunService Instance { get { if (_instance == null) { _instance = new TimerRunService(); } return _instance; } } #endregion #region 启动定时器运行服务 /// /// 启动定时器运行服务 /// public void Start() { if (this._timer == null) { this._timer = new System.Timers.Timer(); this._timer.Interval = 5000; this._timer.Elapsed += new System.Timers.ElapsedEventHandler(_timer_Elapsed); this._timer.Start(); } } #endregion #region 停止定时器运行服务 /// /// 停止定时器运行服务 /// public void Stop() { if (this._timer != null) { this._timer.Stop(); this._timer.Dispose(); this._timer = null; } } #endregion #region 定时器服务事件处理 /// /// 定时器服务事件处理 /// /// /// protected void _timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e) { lock (String.Empty) { this._cnt++; SysVersionController.ProcessVersion(); //处理版本信息 #region 1、更新网络表中改设备的运行状态,移到计时器服务类中 FeedingPlc.UpdateEquipState updateEquipStateInstance = new FeedingPlc.UpdateEquipState(); if (updateEquipStateInstance.NetType == Mesnac.Action.Base.BaseAction.NetTypes.Net) { updateEquipStateInstance.Run(null); } #endregion #region 检测Socket是否在侦听,如果没有侦听,则重启侦听服务(每5次循环,检测一次端口占用) if (this._cnt > 5) { //每5次循环,检测一次端口占用 int port = 3666; object oPort = ConfigurationManager.AppSettings["Port"]; if (oPort != null) { int.TryParse(oPort.ToString(), out port); } if (!Mesnac.Basic.ProcessHelper.PortInUse(port)) { Mesnac.Communication.TcpService.ReStartService(); //重新启动Socket侦听 } this._cnt = 0; } #endregion } } #endregion } }