diff --git a/DM_Weight/App.config b/DM_Weight/App.config index 26cbf26..27b072d 100644 --- a/DM_Weight/App.config +++ b/DM_Weight/App.config @@ -37,7 +37,7 @@ - + diff --git a/DM_Weight/App.xaml b/DM_Weight/App.xaml index ef8d445..704fc50 100644 --- a/DM_Weight/App.xaml +++ b/DM_Weight/App.xaml @@ -6,7 +6,7 @@ d1p1:Ignorable="d" xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes" xmlns:local="clr-namespace:DM_Weight" - xmlns:prism="http://prismlibrary.com/"> + xmlns:prism="http://prismlibrary.com/" Startup="PrismApplication_Startup"> diff --git a/DM_Weight/App.xaml.cs b/DM_Weight/App.xaml.cs index 66800c5..0d26a25 100644 --- a/DM_Weight/App.xaml.cs +++ b/DM_Weight/App.xaml.cs @@ -24,6 +24,8 @@ using System.Windows.Interop; using System.Windows.Threading; using System.Timers; using DM_Weight.HIKVISION; +using System.Runtime.InteropServices; +using System.Diagnostics; namespace DM_Weight { @@ -291,5 +293,45 @@ namespace DM_Weight logger.Info("结束APP-RegisterTypes"); } + + private void PrismApplication_Startup(object sender, StartupEventArgs e) + { + //获取欲启动程序名 + string processName = System.Diagnostics.Process.GetCurrentProcess().ProcessName; + logger.Info($"欲启动程序名:{processName}"); + //检查程序是否已经启动,已经启动则显示提示退出程序 + if (System.Diagnostics.Process.GetProcessesByName(processName).Length > 1) + { + + logger.Info($"系统在运行!"); + //系统在运行 + RaiseOtherProcess(); + Application.Current.Shutdown(); + return; + } + } + private static void RaiseOtherProcess() + { + Process proc = Process.GetCurrentProcess(); + foreach (Process otherProc in Process.GetProcessesByName(Process.GetCurrentProcess().ProcessName)) + { + if (proc.Id != otherProc.Id) + { + IntPtr hWnd = otherProc.MainWindowHandle; + if (IsIconic(hWnd)) + { + ShowWindowAsync(hWnd, 9); + } + SetForegroundWindow(hWnd); + break; + } + } + } + [DllImport("user32.dll")] + private static extern bool SetForegroundWindow(IntPtr hWnd); + [DllImport("user32.dll")] + private static extern bool ShowWindowAsync(IntPtr hWnd, int nCmdShow); + [DllImport("user32.dll")] + private static extern bool IsIconic(IntPtr hWnd); } } diff --git a/DM_Weight/DM_Weight.csproj b/DM_Weight/DM_Weight.csproj index 30703a6..aa44d2f 100644 --- a/DM_Weight/DM_Weight.csproj +++ b/DM_Weight/DM_Weight.csproj @@ -1,7 +1,7 @@  - Exe + WinExe net6.0-windows enable true diff --git a/DM_Weight/ViewModels/HomeWindowViewModel.cs b/DM_Weight/ViewModels/HomeWindowViewModel.cs index 346e971..6cd7175 100644 --- a/DM_Weight/ViewModels/HomeWindowViewModel.cs +++ b/DM_Weight/ViewModels/HomeWindowViewModel.cs @@ -527,6 +527,8 @@ namespace DM_Weight.ViewModels //接收导航传过来的参数 public void OnNavigatedTo(NavigationContext navigationContext) { + + _eventAggregator.GetEvent().Subscribe(SetLoginOut); _portUtil.dateTime = DateTime.Now; //取出user UserList = navigationContext.Parameters.GetValue("operator"); @@ -554,6 +556,7 @@ namespace DM_Weight.ViewModels Type = MsgType.ERROR }; _eventAggregator.GetEvent().Publish(alertMsg); + return; } //SqlSugarHelper.Db.SqlQueryable(sql) @@ -566,47 +569,47 @@ namespace DM_Weight.ViewModels int autoExit = Convert.ToInt32(ConfigurationManager.AppSettings["autoExit"] ?? "0"); int stopRecord = Convert.ToInt32(ConfigurationManager.AppSettings["stopRecord"] ?? "0"); - if (autoExit > 0) - { - System.Timers.Timer timer = new System.Timers.Timer(); - timer.Interval = 100; - timer.Elapsed += (sender, e) => - { - // 串口无人操作 - bool[] boolArrs = ModbusHelper.GetInstance().GetAllBoxState(); - bool allTrue = Array.TrueForAll(boolArrs, b => b==false); - logger.Info($"进入自动退出定时方法{allTrue}"); - //false是关着,true是开着 - if (allTrue) - { - //PerformanceCounter pc = new PerformanceCounter("System", "% Idle Time", true); - //float systemIdleTime = pc.NextValue(); - //Console.WriteLine($"systemIdleTime:{systemIdleTime}"); + //if (autoExit > 0) + //{ + // System.Timers.Timer timer = new System.Timers.Timer(); + // timer.Interval = 100; + // timer.Elapsed += (sender, e) => + // { + // // 串口无人操作 + // bool[] boolArrs = ModbusHelper.GetInstance().GetAllBoxState(); + // bool allTrue = Array.TrueForAll(boolArrs, b => b==false); + // logger.Info($"进入自动退出定时方法{allTrue}"); + // //false是关着,true是开着 + // if (allTrue) + // { + // //PerformanceCounter pc = new PerformanceCounter("System", "% Idle Time", true); + // //float systemIdleTime = pc.NextValue(); + // //Console.WriteLine($"systemIdleTime:{systemIdleTime}"); - //// 获取系统空闲时间 - //TimeSpan idleTime = SystemInformation.IdleTime; - //// 打印空闲时间 - //Console.WriteLine("系统空闲时间: " + idleTime.ToString()); - long lTime = CheckComputerFreeState.GetLastInputTime(); - logger.Info($"自动退出时间{lTime}"); - // 30秒内无人操作鼠标键盘 - //if ((DateTime.Now - _portUtil.dateTime).TotalSeconds > autoExit && CheckComputerFreeState.GetLastInputTime() > autoExit) - if (lTime > autoExit) - { - logger.Info($"设备{autoExit}秒内无人操作,用户【{Operator?.Nickname}】自动退出登录"); - _chkFunction.HIKStopDVRRecord(); - Operator = null; - Reviewer = null; - Application.Current.Dispatcher.Invoke(() => - { - _regionManager.RequestNavigate("MainRegion", "LoginWindow"); - timer.Stop(); - }); - } - } - }; - timer.Start(); - } + // //// 获取系统空闲时间 + // //TimeSpan idleTime = SystemInformation.IdleTime; + // //// 打印空闲时间 + // //Console.WriteLine("系统空闲时间: " + idleTime.ToString()); + // long lTime = CheckComputerFreeState.GetLastInputTime(); + // logger.Info($"自动退出时间{lTime}"); + // // 30秒内无人操作鼠标键盘 + // //if ((DateTime.Now - _portUtil.dateTime).TotalSeconds > autoExit && CheckComputerFreeState.GetLastInputTime() > autoExit) + // if (lTime > autoExit) + // { + // logger.Info($"设备{autoExit}秒内无人操作,用户【{Operator?.Nickname}】自动退出登录"); + // _chkFunction.HIKStopDVRRecord(); + // Operator = null; + // Reviewer = null; + // Application.Current.Dispatcher.Invoke(() => + // { + // _regionManager.RequestNavigate("MainRegion", "LoginWindow"); + // timer.Stop(); + // }); + // } + // } + // }; + // timer.Start(); + //} #region 温度查询定时 int interval = Convert.ToInt32(ConfigurationManager.AppSettings["Interval"]); if (interval > 0) @@ -650,7 +653,19 @@ namespace DM_Weight.ViewModels //这个方法用于拦截请求 public void OnNavigatedFrom(NavigationContext navigationContext) { - + //退了登录 + _eventAggregator.GetEvent().Unsubscribe(SetLoginOut); } + private void SetLoginOut() + { + _chkFunction.HIKStopDVRRecord(); + Operator = null; + Reviewer = null; + Application.Current.Dispatcher.Invoke(() => + { + _regionManager.RequestNavigate("MainRegion", "LoginWindow"); + }); + } + } } diff --git a/DM_Weight/Views/HomeWindow.xaml.cs b/DM_Weight/Views/HomeWindow.xaml.cs index fe2e7d1..265a17a 100644 --- a/DM_Weight/Views/HomeWindow.xaml.cs +++ b/DM_Weight/Views/HomeWindow.xaml.cs @@ -1,8 +1,15 @@ -using System; +using DM_Weight.msg; +using DM_Weight.Port; +using log4net.Repository.Hierarchy; +using Prism.Events; +using System; 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 System.Windows.Data; @@ -20,9 +27,40 @@ namespace DM_Weight.Views /// public partial class HomeWindow : UserControl { - public HomeWindow() + int autoExit = Convert.ToInt32(ConfigurationManager.AppSettings["autoExit"] ?? "0"); + System.Timers.Timer idleTimer; + IEventAggregator _eventAggregator; + public HomeWindow(IEventAggregator eventAggregator) { InitializeComponent(); + if (autoExit > 0) + { + idleTimer = new System.Timers.Timer(autoExit*1000); + idleTimer.Elapsed += OnTimerElapsed; + this.MouseDown += OnUserActivity; + this.MouseMove += OnUserActivity; + this.KeyDown += OnUserActivity; + idleTimer.Start(); + _eventAggregator = eventAggregator; + } } + private void OnUserActivity(object sender, EventArgs e) + { + idleTimer.Stop(); + idleTimer.Start(); + } + private void OnTimerElapsed(object sender, ElapsedEventArgs e) + { + // 串口无人操作 + bool[] boolArrs = ModbusHelper.GetInstance().GetAllBoxState(); + bool allTrue = Array.TrueForAll(boolArrs, b => b == false); + //logger.Info($"进入自动退出定时方法{allTrue}"); + if (allTrue) + { + //无人操作,自动退出 + _eventAggregator.GetEvent().Publish(); + } + } + } } diff --git a/DM_Weight/msg/LoginOutEvent.cs b/DM_Weight/msg/LoginOutEvent.cs new file mode 100644 index 0000000..d582a5d --- /dev/null +++ b/DM_Weight/msg/LoginOutEvent.cs @@ -0,0 +1,13 @@ +using Prism.Events; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace DM_Weight.msg +{ + public class LoginOutEvent:PubSubEvent + { + } +}