From c419cb2a84162bd34acf6baabe29bd6ec864e75d Mon Sep 17 00:00:00 2001 From: "nodyang@aliyun.com" Date: Fri, 22 Nov 2024 16:06:43 +0800 Subject: [PATCH] PlcConnect.IsSuccess --- RfidWeb/FormMain.cs | 35 +++++++++++++++++++++++++++++------ Tool/PlcConnect.cs | 10 ++-------- 2 files changed, 31 insertions(+), 14 deletions(-) diff --git a/RfidWeb/FormMain.cs b/RfidWeb/FormMain.cs index efd2a18..5b55826 100644 --- a/RfidWeb/FormMain.cs +++ b/RfidWeb/FormMain.cs @@ -15,6 +15,8 @@ using System; using System.Collections.Generic; using System.Drawing; using System.Security.Policy; +using System.Threading; +using System.Threading.Tasks; using System.Windows.Forms; using Tool; @@ -54,7 +56,8 @@ namespace RfidWeb private void Init() { - + + ShowLed(); _timer = new TimerX(TimeState, null, 2000, 1000); _timer.Async=true; // 如果所有的日志在记录之前需要在控制台显示出来 @@ -81,6 +84,9 @@ namespace RfidWeb /// 移除过期的缓存项 void TimeState(Object state) { + + + bool isExit = UserManager.IsExit(); this.Invoke(() => { @@ -110,7 +116,8 @@ namespace RfidWeb }); - ThreadPoolX.QueueUserWorkItem(ShowLed); + if (!PlcConnect.IsSuccess) return; + ThreadPoolX.QueueUserWorkItem(UpdateLog); ThreadPoolX.QueueUserWorkItem(ReadBufLog); @@ -121,12 +128,28 @@ namespace RfidWeb private void ShowLed() { - CacheManager.Act(CacheKeyManager.Home, () => + + var _cancellationTokenSource = new CancellationTokenSource(); + var token = _cancellationTokenSource.Token; + + Task.Run(async () => { + while (!token.IsCancellationRequested) + { + // 在这里执行你的轮询逻辑 + var result =await PlcConnect.Instance.ReadPlcTypeAsync(); + hslLanternSimplePlc.LanternBackground = result.IsSuccess + ? Color.LimeGreen : Color.Red; + // 在主线程更新 UI + //Invoke(new Action(() => + //{ + + //})); + PlcConnect.IsSuccess = result.IsSuccess; + await Task.Delay(1000, token); // 每隔一秒延时 + } + }, token); - bool result = PlcConnect.IsSuccess; - hslLanternSimplePlc.LanternBackground = result ? Color.LimeGreen : Color.Red; - }); } private void ReadBufLog() diff --git a/Tool/PlcConnect.cs b/Tool/PlcConnect.cs index 86e5649..de7cff0 100644 --- a/Tool/PlcConnect.cs +++ b/Tool/PlcConnect.cs @@ -104,14 +104,8 @@ namespace Tool } - public static bool IsSuccess - { - get - { - bool result = PlcConnect.Instance.ReadPlcType().IsSuccess; - return result; - } - } + public static bool IsSuccess; + public static OperateResult WriteTag(string address, UInt32 num)