2021-09-04 WPF上位机通用框架平台实战-全局监控对象
Posted 微软MVP Eleven
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了2021-09-04 WPF上位机通用框架平台实战-全局监控对象相关的知识,希望对你有一定的参考价值。
在程序启动的时候就实时监控设备
public partial class App : Application
{
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
GlobalMonitor.Start();
if (new LoginWindow().ShowDialog() == true)
{
new MainWindow().ShowDialog();
}
Application.Current.Shutdown();
}
protected override void OnExit(ExitEventArgs e)
{
GlobalMonitor.Stop();
base.OnExit(e);
}
}
public class GlobalMonitor
{
public static ObservableCollection<DeviceModel> DeviceList { get; set; } = new ObservableCollection<DeviceModel>();
static bool isRunning = true;
static Task mainTask = null;
public static void Start()
{
mainTask = Task.Run(async () =>
{
// 获取设备信息
//DeviceList.Add(new DeviceModel { Name = "#1 Master device info" });
//DeviceList.Add(new DeviceModel { Name = "#2 Master device info" });
//DeviceList.Add(new DeviceModel { Name = "#3 Master device info" });
//DeviceList.Add(new DeviceModel { Name = "#4 Master device info" });
DeviceService deviceService = new DeviceService();
var list = deviceService.GetDevices();
if (list != null)
foreach (var item in list)
{
DeviceList.Add(item);
}
//DeviceList = new ObservableCollection<DeviceModel>(list);
while (isRunning)
{
await Task.Delay(100);
foreach (var item in DeviceList)
{
if (item.CommType == 2 && item.S7 != null)// S7通信
{
Zhaoxi.Communication.Siemens.S7Net s7Net = new Communication.Siemens.S7Net(item.S7.IP, item.S7.Port, (byte)item.S7.Rock, (byte)item.S7.Slot);
List<string> addrList = item.MonitorValueList.Select(v => v.Address).ToList();
var result = s7Net.Read<ushort>(addrList);
if (result.IsSuccessed)
{
for (int i = 0; i < item.MonitorValueList.Count; i++)
{
item.MonitorValueList[i].Value = result.Datas[i];
}
}
s7Net.Close();
}
}
}
});
}
public static void Stop()
{
isRunning = false;
mainTask.ConfigureAwait(true);
}
}
以上是关于2021-09-04 WPF上位机通用框架平台实战-全局监控对象的主要内容,如果未能解决你的问题,请参考以下文章
2021-09-04 WPF上位机通用框架平台实战-Device
2021-09-02 WPF上位机通用框架平台实战-项目架构图
2021-09-03 WPF上位机通用框架平台实战-Dashboard