From 6d5a72b849f6afffaccb3ce46aa47a0180fdc49f Mon Sep 17 00:00:00 2001 From: tayttt Date: Fri, 26 Jun 2026 00:32:56 +0800 Subject: [PATCH] fix: handle login window shutdown --- Txgy.EWS.Client.Common/Helpers/VisualHost.cs | 49 ++++++++++++++++--- .../ViewModels/EarlyWarningViewModel.cs | 40 ++++++++++----- Txgy.EWS.Client.Start/App.xaml.cs | 29 ++++++----- Txgy.EWS.Client.Start/Views/LoginView.xaml.cs | 2 +- 4 files changed, 88 insertions(+), 32 deletions(-) diff --git a/Txgy.EWS.Client.Common/Helpers/VisualHost.cs b/Txgy.EWS.Client.Common/Helpers/VisualHost.cs index 8dff49b..3cca61b 100644 --- a/Txgy.EWS.Client.Common/Helpers/VisualHost.cs +++ b/Txgy.EWS.Client.Common/Helpers/VisualHost.cs @@ -28,19 +28,54 @@ namespace Txgy.EWS.Client.Common.Helpers //添加Visual public void AddVisual(Visual visual) { - visuals.Add(visual); - - Application.Current.Dispatcher.Invoke(() => base.AddVisualChild(visual)); - Application.Current.Dispatcher.Invoke(() => base.AddLogicalChild(visual)); + InvokeOnDispatcher(() => + { + if (visual == null) + { + return; + } + + visuals.Add(visual); + base.AddVisualChild(visual); + base.AddLogicalChild(visual); + }); } //删除Visual public void RemoveVisual(Visual visual) { - visuals.Remove(visual); + InvokeOnDispatcher(() => + { + if (visual == null) + { + return; + } + + if (!visuals.Remove(visual)) + { + return; + } + + base.RemoveVisualChild(visual); + base.RemoveLogicalChild(visual); + }); + } + + private void InvokeOnDispatcher(Action action) + { + var dispatcher = Dispatcher; + if (dispatcher == null || dispatcher.HasShutdownStarted || dispatcher.HasShutdownFinished) + { + return; + } + + if (dispatcher.CheckAccess()) + { + action(); + return; + } - Application.Current.Dispatcher.Invoke(()=>base.RemoveVisualChild(visual)); - Application.Current.Dispatcher.Invoke(() => base.RemoveLogicalChild(visual)); + dispatcher.Invoke(action); } //命中测试 diff --git a/Txgy.EWS.Client.PageModule/ViewModels/EarlyWarningViewModel.cs b/Txgy.EWS.Client.PageModule/ViewModels/EarlyWarningViewModel.cs index 52b7021..aaada0f 100644 --- a/Txgy.EWS.Client.PageModule/ViewModels/EarlyWarningViewModel.cs +++ b/Txgy.EWS.Client.PageModule/ViewModels/EarlyWarningViewModel.cs @@ -611,7 +611,7 @@ namespace Txgy.EWS.Client.PageModule.ViewModels //InitEventList(); this._ea.GetEvent().Subscribe(u => { - Application.Current.Dispatcher.Invoke(new System.Action(() => + InvokeOnDispatcher(() => { ListEventFre.Add(new ObservableValue(u)); EventFrequencyCount = u; @@ -621,12 +621,12 @@ namespace Txgy.EWS.Client.PageModule.ViewModels } EventFreLabels.Add(DateTime.Now.ToString("HH:mm")); EventFreMaxY = (int)(ListEventFre.Max(le => le.Value) + 4); - })); + }); }); //最后预警事件 this._ea.GetEvent().Subscribe(u => { - Application.Current.Dispatcher.Invoke(new System.Action(() => + InvokeOnDispatcher(() => { LastAlarmEventEnergy = u.Energy; ListEnergy.Add(new ObservableValue(u.Energy)); @@ -639,7 +639,7 @@ namespace Txgy.EWS.Client.PageModule.ViewModels //int domainFreq = rnd.Next(15, 60); //LastAlarmEventDominFreq = domainFreq; //ListDominantFreq.Add(new ObservableValue(domainFreq)); - })); + }); }); //事件主频 this._ea.GetEvent().Subscribe(u => @@ -664,7 +664,7 @@ namespace Txgy.EWS.Client.PageModule.ViewModels //从接收事件列表中删除超过指定时间范围的数据 var timeoutReceivedEvents = ReceviedEvents.Where(re => (DateTime.Now - DateTime.Parse(re.EventTime)).TotalHours >= GlobalConfig.DataLookbackHours).ToArray(); - _dispatcher.Invoke(new System.Action(() => + InvokeOnDispatcher(() => { if (timeoutReceivedEvents.Length > 0) { @@ -674,12 +674,12 @@ namespace Txgy.EWS.Client.PageModule.ViewModels ReceviedEvents.Remove(dg); } } - })); + }); } - _dispatcher.Invoke(new System.Action(() => + InvokeOnDispatcher(() => { ReceviedEvents.Insert(0, gier); - })); + }); //ReceivedEvents = new ObservableCollection(); //AlarmEvents = new ObservableCollection(); @@ -692,7 +692,7 @@ namespace Txgy.EWS.Client.PageModule.ViewModels { var timeoutAlarmEvents = AlarmEvents.Where(re => (DateTime.Now - DateTime.Parse(re.EventTime)).TotalHours >= GlobalConfig.DataLookbackHours).ToArray(); - _dispatcher.Invoke(new System.Action(() => + InvokeOnDispatcher(() => { if (timeoutAlarmEvents.Length > 0) { @@ -702,18 +702,34 @@ namespace Txgy.EWS.Client.PageModule.ViewModels AlarmEvents.Remove(dg); } } - })); + }); } - _dispatcher.Invoke(new System.Action(() => + InvokeOnDispatcher(() => { AlarmEvents.Insert(0, gier); - })); + }); //ReceivedEvents = new ObservableCollection(); //AlarmEvents = new ObservableCollection(); }); } + private void InvokeOnDispatcher(System.Action action) + { + if (action == null || _dispatcher == null || _dispatcher.HasShutdownStarted || _dispatcher.HasShutdownFinished) + { + return; + } + + if (_dispatcher.CheckAccess()) + { + action(); + return; + } + + _dispatcher.Invoke(action); + } + private void ClearWarningData() { AlarmEvents.Clear(); diff --git a/Txgy.EWS.Client.Start/App.xaml.cs b/Txgy.EWS.Client.Start/App.xaml.cs index 63631c8..22676ef 100644 --- a/Txgy.EWS.Client.Start/App.xaml.cs +++ b/Txgy.EWS.Client.Start/App.xaml.cs @@ -32,6 +32,8 @@ namespace Txgy.EWS.Client.Start /// public partial class App : PrismApplication { + private bool _loginSucceeded; + public App() { // 日志Log4Net初始化 @@ -62,13 +64,25 @@ namespace Txgy.EWS.Client.Start { var config = BusinessConfigManager.Current; GlobalConfig.InitializeRuntimeSettings(config.Runtime); + GlobalConfig.ReadConfig(); //Debug.WriteLine(GlobalConfig.LoadDataTimeLenMins); //Debug.WriteLine(GlobalConfig.DataCacheTimeLenMins); + _loginSucceeded = Container.Resolve().ShowDialog() == true; + if (!_loginSucceeded) + { + Shutdown(); + return null; + } + return Container.Resolve(); } protected override void InitializeShell(Window shell) { - GlobalConfig.ReadConfig(); + if (!_loginSucceeded || shell == null) + { + return; + } + //if (GlobalData.IsDesign) //{ // base.InitializeShell(shell); @@ -76,17 +90,8 @@ namespace Txgy.EWS.Client.Start //else { //GlobalConfig.ReadConfig(); - // 通信容器来获取对象 可以进行相关对象的注入 - if (Container.Resolve().ShowDialog() == false) - { - //GlobalConfig.ReadConfig(); - Application.Current?.Shutdown(); - } - else - { - //CommonLogHelper.Debug("====Start=====>"); - base.InitializeShell(shell); - } + //CommonLogHelper.Debug("====Start=====>"); + base.InitializeShell(shell); } } protected override void RegisterTypes(IContainerRegistry containerRegistry) diff --git a/Txgy.EWS.Client.Start/Views/LoginView.xaml.cs b/Txgy.EWS.Client.Start/Views/LoginView.xaml.cs index 84afead..30c8e2b 100644 --- a/Txgy.EWS.Client.Start/Views/LoginView.xaml.cs +++ b/Txgy.EWS.Client.Start/Views/LoginView.xaml.cs @@ -45,7 +45,7 @@ namespace Txgy.EWS.Client.Start.Views private void Button_Click(object sender, RoutedEventArgs e) { - this.Close(); + this.DialogResult = false; } } }