using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Bro.Process; namespace Bro.M141.Process { public partial class M141Process { static public System.Threading.Timer Timer; private void SetTaskAtFixedTime() { //System.Threading.Timer timer = new System.Threading.Timer(new System.Threading.TimerCallback(SaveDataByWorkShift(dateTime)), null, 1*60*1000); //Task task = new Task(() => //{ // timer //}); //task.Start(); //Task.Factory.StartNew(() => //{ // System.Threading.Thread.Sleep(1*60*1000); // SaveDataByWorkShift(dateTime); //}); Timer = new System.Threading.Timer(SaveDataByWorkShift, 1, 0, Timeout.Infinite); } public void SaveDataByWorkShift(object obj) { try { string dateTime = DateTime.Now.ToString("HH:mm"); var workShiftSetting = M141Config.WorkShiftList; if (workShiftSetting.Count == 0) { return; } var curProductAmount = base.StatisticRecordsFull.CurRecord.DefectSummary; if (curProductAmount != null) { workShiftSetting.ForEach(w => { //如果当前时间为该班次的起始时间,且需要清除数据 if (dateTime == w.ShiftTime_Start.ToString("HH:mm") && w.IsClearProductSummary) { LogAsync(DateTime.Now, Common.Helper.EnumHelper.LogLevel.Action, $"班次{w.ShiftName}开始时进行生产数据清零"); curProductAmount.Reset(); } //如果当前时间为该班次的结束时间,且需要保存数据 else if (dateTime == w.ShiftTime_End.ToString("HH:mm") && w.IsRecordProductSummary) { LogAsync(DateTime.Now, Common.Helper.EnumHelper.LogLevel.Action, $"班次{w.ShiftName}结束时进行生产数据保存"); string savePath = Path.Combine(M141Config.LogPath, "班次记录"); if (!Directory.Exists(savePath)) { Directory.CreateDirectory(savePath); } string fileName = $"{w.ShiftName}_{DateTime.Now.ToString("yyyyMMdd")}_#{w.ShiftTime_Start.ToString("HHmm")}-{w.ShiftTime_End.ToString("HHmm")}#.csv", pHead = "检测项,数量,占比", pData = ""; curProductAmount.RecordsList.ForEach(e => { pData += $"{e.ClassDesc},{e.Amount},{e.PercentStr},\n"; }); CSVRecordAsync(Path.Combine(savePath, fileName), pData, pHead); } }); } } catch (Exception) { Timer.Dispose(); } finally { Timer.Change(1 * 60 * 1000, Timeout.Infinite); } } } }