plcsharp/PLCSharpWpf/VVMs/Cylinders/CylinderModel.cs
tfarcraw b97083f556 修复异常被静默吞掉:补 UI 线程兜底 + 给 17 个轮询线程装上异常报警
两条异常「消失」的路径:
1) App.RegisterEvents 只挂了 TaskScheduler.UnobservedTaskException 与 AppDomain.UnhandledException,
   全项目没有 Application.DispatcherUnhandledException。WPF UI 线程里的异常(视觉 handler 里 33 处
   Dispatcher.Invoke 委托内部抛出即属此类)会直接让进程闪退,只留一行日志。
2) 17 个 BackgroundWorker 轮询线程全都只订阅 DoWork。BackgroundWorker 会捕获 DoWork 的异常并放进
   RunWorkerCompletedEventArgs.Error,没人订阅就等于吞掉:while(!CancellationPending) 循环直接退出,
   轴/IO/通讯/机器人/气缸的扫描从此不再刷新,而界面看起来一切正常 —— 对上位机这是最危险的失败模式。

改法:
- 新增 Core/Prism/WorkerGuard:Watch(worker, 用途名) 给 worker 挂上「因异常结束就必须上报」的兜底,
  上报出口由启动时 SetFaultSink 注入(Core 层不依赖 App);正常取消(e.Error==null)不算故障,不上报;
  上报通道自身异常被吞掉,兜底逻辑不成为新的崩溃源。
- App.xaml.cs:注册 DispatcherUnhandledException —— 记录日志 + 走统一 errLog 报警通道 + Handled=true
  让进程继续存活(设备运行中闪退比界面某处出错危险得多);OutOfMemoryException 不拦截,交回运行时终止;
  ReportError 带重入保护,避免「报警通道自己出错」造成无限递归。errLog 通道此前只有 ModelBase 在用。
- 17 个 new BackgroundWorker 构造处统一包成 WorkerGuard.Watch(...),并给每个轮询起了中文名(报警里能看出是谁死了)。
- 顺带删掉 App.xaml.cs 里一处没有配对 disable 的孤立 #pragma warning restore VSTHRD200。

新增 5 个 WorkerGuard 回归测试(异常上报内容 / 正常结束不上报 / 正常取消不上报 / 出口抛异常不外泄 / null 参数)。构建 0 error,测试 54/54 通过。
2026-11-17 23:21:05 +08:00

189 lines
6.5 KiB
C#

using PLCSharp.Core.Prism;
using PLCSharp.Models;
using Prism.Dialogs;
using Prism.Events;
using Prism.Ioc;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Threading;
namespace PLCSharp.VVMs.Cylinders
{
/// <summary>
/// 气缸模型
/// </summary>
[Model]
public class CylinderModel : ModelBase
{
/// <summary>
/// 气缸模型
/// </summary>
public CylinderModel(IContainerExtension container, IEventAggregator ea, IDialogService dialogService) : base(container, ea, dialogService)
{
bkgWorker = WorkerGuard.Watch(new BackgroundWorker { WorkerSupportsCancellation = true }, "气缸轮询");
bkgWorker.DoWork += BackgroundWork;
if (!bkgWorker.IsBusy)
bkgWorker.RunWorkerAsync();
}
private readonly BackgroundWorker bkgWorker;
private void BackgroundWork(object sender, DoWorkEventArgs e)
{
var worker = (BackgroundWorker)sender;
while (!worker.CancellationPending)
{
Thread.Sleep(50);
try
{
// 快照遍历,避免与 UI 线程增删气缸产生集合并发问题
var list = Cylinders.ToList();
for (int i = 0; i < list.Count; i++)
{
if (list[i] == null
|| list[i].Enabled == false)//未启的气缸不进行处理
continue;
var controller = GlobalModel.ControllersModel.Controller;
if (controller == null) continue;
// 更新 DI DQ
if (list[i].SensorType == SensorType.Two || list[i].SensorType == SensorType.OnlyA)
{
if (string.IsNullOrEmpty(list[i].InA) == false)
{
if (list[i].DIA == null || list[i].DIA.Name != list[i].InA)
list[i].DIA = controller.DI.FirstOrDefault(d => d.Name == list[i].InA);
}
}
if (string.IsNullOrEmpty(list[i].OutA) == false)
{
if (list[i].DQA == null || list[i].DQA.Name != list[i].OutA)
list[i].DQA = controller.DQ.FirstOrDefault(d => d.Name == list[i].OutA);
}
if (list[i].SensorType == SensorType.Two || list[i].SensorType == SensorType.OnlyB)
{
if (string.IsNullOrEmpty(list[i].InB) == false)
{
if (list[i].DIB == null || list[i].DIB.Name != list[i].InB)
list[i].DIB = controller.DI.FirstOrDefault(d => d.Name == list[i].InB);
}
}
if (string.IsNullOrEmpty(list[i].OutB) == false)
{
if (list[i].DQB == null || list[i].DQB.Name != list[i].OutB)
list[i].DQB = controller.DQ.FirstOrDefault(d => d.Name == list[i].OutB);
}
//气缸功能块
FB(list[i]);
}
}
catch (Exception ex)
{
SendErr(ex.Message);
}
}
}
private void FB(Cylinder cylinder)
{
//如果气缸没有传感器 则判断气缸输出延时是否完成
if (cylinder.SensorType == SensorType.None || cylinder.SensorType == SensorType.OnlyB)
{
if (cylinder.DQA != null && cylinder.DQA.Status)
{
cylinder.AComplit = cylinder.ADelayTimer.AddSeconds(cylinder.ADelay) < DateTime.Now;
}
else
{
cylinder.AComplit = false;
cylinder.ADelayTimer = DateTime.Now;
}
}
if (cylinder.SensorType == SensorType.None || cylinder.SensorType == SensorType.OnlyA)
{
if (cylinder.DQB != null && cylinder.DQB.Status)
{
cylinder.BComplit = cylinder.BDelayTimer.AddSeconds(cylinder.BDelay) < DateTime.Now;
}
else
{
cylinder.BComplit = false;
cylinder.BDelayTimer = DateTime.Now;
}
}
//如果气缸有传感器 则判断传感器倒位延时是否完成
if (cylinder.SensorType == SensorType.Two || cylinder.SensorType == SensorType.OnlyA)
{
if (cylinder.DIA != null && cylinder.DIA.Status)
{
cylinder.AComplit = cylinder.ADelayTimer.AddSeconds(cylinder.ADelay) < DateTime.Now;
}
else
{
cylinder.AComplit = false;
cylinder.ADelayTimer = DateTime.Now;
}
}
if (cylinder.SensorType == SensorType.Two || cylinder.SensorType == SensorType.OnlyB)
{
if (cylinder.DIB != null && cylinder.DIB.Status)
{
cylinder.BComplit = cylinder.BDelayTimer.AddSeconds(cylinder.BDelay) < DateTime.Now;
}
else
{
cylinder.BComplit = false;
cylinder.BDelayTimer = DateTime.Now;
}
}
}
/// <summary>
/// 全局模型
/// </summary>
public GlobalModel GlobalModel { get; set; }
private ObservableCollection<Cylinder> _Cylinders = [];
/// <summary>
/// 气缸列表
/// </summary>
public ObservableCollection<Cylinder> Cylinders
{
get { return _Cylinders; }
set { SetProperty(ref _Cylinders, value); }
}
/// <summary>
/// 初始化
/// </summary>
/// <param name="globalModel">全局模型</param>
public void Init(GlobalModel globalModel)
{
GlobalModel = globalModel;
foreach (var item in _DatasContext.Cylinders)
{
Cylinders.Add(item);
}
}
}
}