diff --git a/ButcherFactory.Form/SegmentProductionAuto_/SegmentProductionAutoForm.cs b/ButcherFactory.Form/SegmentProductionAuto_/SegmentProductionAutoForm.cs index 69ba1e5..191ae1b 100644 --- a/ButcherFactory.Form/SegmentProductionAuto_/SegmentProductionAutoForm.cs +++ b/ButcherFactory.Form/SegmentProductionAuto_/SegmentProductionAutoForm.cs @@ -48,18 +48,34 @@ namespace ButcherFactory.SegmentProductionAuto_ List identityList; List specialGoodsCodes; - // 优雅关闭标记 + private bool _isFormClosing = false; public WeightDetails mCache; public Dictionary> wDic = new Dictionary>(); + // ----------------------------------------------------- + // 类内新增字段 + private System.Windows.Forms.Timer _msgClearTimer; + + // 构造函数里面加初始化 public SegmentProductionAutoForm() { InitializeComponent(); butcherDateBox.Text = butcherDate.ToString("yyyy-MM-dd"); netStateWatch1.GetConnectState = () => LoginUtil.TestConnection(500); this.FormClosing += SegmentProductionAutoForm_FormClosing; + + // 消息自动清除定时器,先实例化,不启动 + _msgClearTimer = new System.Windows.Forms.Timer(); + _msgClearTimer.Interval = 5000; + _msgClearTimer.Tick += (s, e) => + { + if (IsDisposed) return; + msglbl.Text = string.Empty; + _msgClearTimer.Stop(); + }; + workUnitSelect.SelectedIndexChanged += delegate { if (workUnitSelect.SelectedValue == null) @@ -84,22 +100,44 @@ namespace ButcherFactory.SegmentProductionAuto_ }; } + // 新增方法:显示错误,5秒自动清空 + private void ShowErrorMsg(string msg) + { + if (IsDisposed) return; + this.Invoke(new Action(() => + { + if (IsDisposed) return; + msglbl.Text = "ERROR:" + msg; + _msgClearTimer.Stop(); + _msgClearTimer.Start(); + })); + } + + // FormClosing 里面释放定时器 void SegmentProductionAutoForm_FormClosing(object sender, FormClosingEventArgs e) { _isFormClosing = true; - // 等待线程自行退出,不再Abort + // 释放消息定时器 + if (_msgClearTimer != null) + { + _msgClearTimer.Stop(); + _msgClearTimer.Dispose(); + } + if (initTask != null && initTask.IsAlive) - initTask.Join(1000); + initTask.Join(1500); if (uploadData != null && uploadData.IsAlive) - uploadData.Join(1000); + uploadData.Join(1500); if (checkInStoreState != null && checkInStoreState.IsAlive) - checkInStoreState.Join(1000); + checkInStoreState.Join(1500); if (Utils.mPortOpened) closePort(); } + // ----------------------------------------------------------- + protected override void OnLoad(EventArgs e) { base.OnLoad(e); @@ -153,7 +191,7 @@ namespace ButcherFactory.SegmentProductionAuto_ { while (!ok) { - if (_isFormClosing) return; + if (_isFormClosing || IsDisposed) return; try { var changed = BaseInfoSyncRpc.SyncGoodsByTag(ApplyClient.分割品); @@ -164,17 +202,18 @@ namespace ButcherFactory.SegmentProductionAuto_ if (changed || !bind) BindBasicInfo(); - if (!IsDisposed && !Disposing) + if (!IsDisposed) { this.Invoke(new Action(() => { + if (IsDisposed) return; msglbl.Text = string.Empty; })); } } catch (Exception ex) { - if (_isFormClosing) return; + if (_isFormClosing || IsDisposed) return; if (!bind) { BindBasicInfo(); @@ -182,11 +221,12 @@ namespace ButcherFactory.SegmentProductionAuto_ } var msg = ex.Message ?? string.Empty; - if (!IsDisposed && !Disposing) + if (!IsDisposed) { this.Invoke(new Action(() => { - msglbl.Text = "ERROR:" + msg; + if (IsDisposed) return; + ShowErrorMsg(msg); })); } LogUtil.WriteErrorFile(msg, "分割品车间称重计数"); @@ -200,7 +240,7 @@ namespace ButcherFactory.SegmentProductionAuto_ if (_isFormClosing || IsDisposed) return; this.BeginInvoke(new Action(() => { - if (IsDisposed || Disposing) return; + if (IsDisposed) return; productBatchSelect.EBindComboBox(null, 6, "Date"); config = XmlUtil.DeserializeFromFile(); var m = config.WorkUnitID; @@ -607,11 +647,11 @@ namespace ButcherFactory.SegmentProductionAuto_ if (_isFormClosing || IsDisposed) return; var entity = obj as SegmentProduction; var list = SegmentProductionBL.GetProductTask(batchDate ?? DateTime.Today, entity); - if (!IsDisposed && !Disposing) + if (!IsDisposed) { this.Invoke(new Action(() => { - if (IsDisposed || Disposing) return; + if (IsDisposed) return; taskDataGrid.DataSource = list; taskDataGrid.Refresh(); })); @@ -664,7 +704,7 @@ namespace ButcherFactory.SegmentProductionAuto_ { while (true) { - if (_isFormClosing) return; + if (_isFormClosing || IsDisposed) return; try { if (netStateWatch1.NetState) @@ -672,11 +712,11 @@ namespace ButcherFactory.SegmentProductionAuto_ var msg = SegmentProductionBL.UploadSegmentInfo(true); if (!string.IsNullOrEmpty(msg) && msg.EndsWith("重复上传")) { - if (!IsDisposed && !Disposing) + if (!IsDisposed) { this.Invoke(new Action(() => { - if (IsDisposed || Disposing) return; + if (IsDisposed) return; MessageBox.Show(msg + "请检查原因!"); })); } @@ -695,7 +735,7 @@ namespace ButcherFactory.SegmentProductionAuto_ { while (true) { - if (_isFormClosing) return; + if (_isFormClosing || IsDisposed) return; try { if (netStateWatch1.NetState) @@ -713,11 +753,11 @@ namespace ButcherFactory.SegmentProductionAuto_ if (tag.Any()) { SegmentProductionBL.SetInStored(tag.Select(x => x.ID).ToList()); - if (!IsDisposed && !Disposing) + if (!IsDisposed) { this.Invoke(new Action(() => { - if (IsDisposed || Disposing) return; + if (IsDisposed) return; var groupByTotal = tag.GroupBy(x => x.TotalCode_ID); foreach (var item2 in groupByTotal) { @@ -796,14 +836,14 @@ namespace ButcherFactory.SegmentProductionAuto_ } catch (Exception ex) { - if (_isFormClosing) return; - if (!IsDisposed && !Disposing) + if (_isFormClosing || IsDisposed) return; + if (!IsDisposed) { this.Invoke(new Action(() => { - if (IsDisposed || Disposing) return; + if (IsDisposed) return; var msg = ex.Message ?? string.Empty; - msglbl.Text = "ERROR:" + msg; + ShowErrorMsg(msg); LogUtil.WriteErrorFile(msg, "分割品车间称重计数"); })); }