From 33cbdb530e71304135d5977366c8a3fba72d5c03 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=A9=AC=E5=B7=A7?= <625215135@qq.com> Date: Mon, 25 Aug 2025 13:29:21 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E8=87=AA=E5=8A=A8=E9=80=80?= =?UTF-8?q?=E5=87=BA=E7=9B=B8=E5=85=B3=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- DM_Weight/App.config | 2 +- DM_Weight/ViewModels/HomeWindowViewModel.cs | 38 ++++++++++++--------- DM_Weight/util/CheckComputerFreeState.cs | 20 +++++++++-- 3 files changed, 40 insertions(+), 20 deletions(-) diff --git a/DM_Weight/App.config b/DM_Weight/App.config index a5d4178..3cbbe58 100644 --- a/DM_Weight/App.config +++ b/DM_Weight/App.config @@ -37,7 +37,7 @@ - + diff --git a/DM_Weight/ViewModels/HomeWindowViewModel.cs b/DM_Weight/ViewModels/HomeWindowViewModel.cs index 5862565..30bf3c2 100644 --- a/DM_Weight/ViewModels/HomeWindowViewModel.cs +++ b/DM_Weight/ViewModels/HomeWindowViewModel.cs @@ -1,4 +1,13 @@ -using log4net; +using DM_Weight.Common; +using DM_Weight.Models; +using DM_Weight.msg; +using DM_Weight.Port; +using DM_Weight.util; +using DM_Weight.Views; +using log4net; +using log4net.Repository.Hierarchy; +using MaterialDesignThemes.Wpf; +using Newtonsoft.Json.Linq; using Prism.Commands; using Prism.Events; using Prism.Mvvm; @@ -10,22 +19,14 @@ using System.Collections.Generic; using System.Configuration; using System.Linq; using System.Text; +using System.Threading; using System.Threading.Tasks; +using System.Timers; using System.Windows; using System.Windows.Controls; -using DM_Weight.Models; -using DM_Weight.Port; -using DM_Weight.util; -using DM_Weight.Views; -using System.Timers; -using Unity; -using System.Windows.Threading; -using Newtonsoft.Json.Linq; -using DM_Weight.msg; -using System.Threading; -using DM_Weight.Common; using System.Windows.Media; -using MaterialDesignThemes.Wpf; +using System.Windows.Threading; +using Unity; namespace DM_Weight.ViewModels { @@ -571,8 +572,9 @@ namespace DM_Weight.ViewModels int autoExit = Convert.ToInt32(ConfigurationManager.AppSettings["autoExit"] ?? "0"); if (autoExit > 0) { + logger.Info("启动自动退出检测"); //int interval = autoExit * 1000; - new PromiseUtil().taskAsyncLoop(1000, 0, async (options, next, stop) => + new PromiseUtil().taskAsyncLoop(10, 0, async (options, next, stop) => { try { @@ -590,8 +592,10 @@ namespace DM_Weight.ViewModels stop(); System.Windows.Application.Current.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Send, new Action(() => { - - DialogHost.Close("RootDialog"); + if (DialogHost.IsDialogOpen("RootDialog")) + { + DialogHost.Close("RootDialog"); + } _regionManager.RequestNavigate("MainRegion", "LoginWindow"); })); //}); @@ -601,10 +605,12 @@ namespace DM_Weight.ViewModels { if (Operator == null) { + logger.Info("自动退出检测已停止"); stop(); } else { + logger.Info($"停止操作时间{_portUtil.dateTime},自动退出时间{autoExit},运算结果{(DateTime.Now - _portUtil.dateTime).TotalSeconds},用户上次使用系统到现在的时间间隔{CheckComputerFreeState.GetLastInputTime()}"); next(); } } diff --git a/DM_Weight/util/CheckComputerFreeState.cs b/DM_Weight/util/CheckComputerFreeState.cs index 1c3f789..6ec9b0d 100644 --- a/DM_Weight/util/CheckComputerFreeState.cs +++ b/DM_Weight/util/CheckComputerFreeState.cs @@ -44,9 +44,23 @@ namespace DM_Weight.util } else { - var count = (Environment.TickCount & Int32.MaxValue) - (long)vLastInputInfo.dwTime; - var icount = count / 1000; - return icount; + //var count = (Environment.TickCount & Int32.MaxValue) - (long)vLastInputInfo.dwTime; + //var icount = count / 1000; + //return icount; + + // 使用 64 位 TickCount 避免回绕问题 + long currentTick = Environment.TickCount64; + long lastInputTick = vLastInputInfo.dwTime; + + // 处理可能的回绕(虽然使用 64 位后极不可能发生) + if (currentTick < lastInputTick) + { + // 发生了回绕,调整计算 + lastInputTick -= uint.MaxValue + 1L; + } + + long elapsedMilliseconds = currentTick - lastInputTick; + return Math.Max(0, elapsedMilliseconds / 1000); } } }