From 1aff8cbc77965d4c9b72aa125c7465fb7efe59d0 Mon Sep 17 00:00:00 2001 From: liuwf Date: Thu, 31 Oct 2024 10:10:02 +0800 Subject: [PATCH] =?UTF-8?q?change-=E4=BC=98=E5=8C=96=E5=BC=82=E5=B8=B8?= =?UTF-8?q?=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- SlnMesnac.WPF/App.xaml.cs | 70 +++++++++++++++++- SlnMesnac.WPF/ViewModel/ProdMgmtViewModel.cs | 77 ++++++++++---------- 2 files changed, 109 insertions(+), 38 deletions(-) diff --git a/SlnMesnac.WPF/App.xaml.cs b/SlnMesnac.WPF/App.xaml.cs index b2617c0..3ad0cb3 100644 --- a/SlnMesnac.WPF/App.xaml.cs +++ b/SlnMesnac.WPF/App.xaml.cs @@ -14,6 +14,7 @@ using System.Text; using SlnMesnac.Common; using System.Net.NetworkInformation; using System.Linq; +using System.Threading.Tasks; namespace SlnMesnac.WPF { @@ -30,6 +31,15 @@ namespace SlnMesnac.WPF protected override async void OnStartup(StartupEventArgs e) { + //UI线程未捕获异常处理事件(UI主线程) + this.DispatcherUnhandledException += App_DispatcherUnhandledException; + + //非UI线程未捕获异常处理事件(例如自己创建的一个子线程) + AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException; + //Task线程内未捕获异常处理事件 + TaskScheduler.UnobservedTaskException += TaskScheduler_UnobservedTaskException;//Task异常 + + bool ret; mutex = new System.Threading.Mutex(true, System.Diagnostics.Process.GetCurrentProcess().ProcessName, out ret); if (!ret) @@ -69,7 +79,65 @@ namespace SlnMesnac.WPF Console.WriteLine(e.Exception.Message); } - + void App_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e) + { + try + { + HandleException(e.Exception); + } + catch (Exception ex) + { + HandleException(ex); + } + finally + { + e.Handled = true; + } + } + private static void HandleException(Exception ex) + { + Log.Warning($"全局异常捕获处理: {ex.Message}"); + + } + //非UI线程未捕获异常处理事件(例如自己创建的一个子线程) + private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e) + { + try + { + var exception = e.ExceptionObject as Exception; + if (exception != null) + { + HandleException(exception); + } + } + catch (Exception ex) + { + HandleException(ex); + } + finally + { + //ignore + } + } + private static void TaskScheduler_UnobservedTaskException(object sender, UnobservedTaskExceptionEventArgs e) + { + try + { + var exception = e.Exception as Exception; + if (exception != null) + { + HandleException(exception); + } + } + catch (Exception ex) + { + HandleException(ex); + } + finally + { + e.SetObserved(); + } + } diff --git a/SlnMesnac.WPF/ViewModel/ProdMgmtViewModel.cs b/SlnMesnac.WPF/ViewModel/ProdMgmtViewModel.cs index 92c7517..ef6b873 100644 --- a/SlnMesnac.WPF/ViewModel/ProdMgmtViewModel.cs +++ b/SlnMesnac.WPF/ViewModel/ProdMgmtViewModel.cs @@ -295,58 +295,61 @@ namespace SlnMesnac.WPF.ViewModel /// 刷新生产计划 /// /// - private void RefreshPlanDataGrid(List list) + private async void RefreshPlanDataGrid(List list) { try { - PlanInfoDataGrid = new ObservableCollection(); - if (list != null) + + await App.Current.Dispatcher.BeginInvoke((Action)(() => { - #region 按钮状态 - // 假设初始状态所有计划的按钮状态 - list.ForEach(plan => + PlanInfoDataGrid = new ObservableCollection(); + if (list != null) { - plan.StartEnable = true; - plan.StopEnable = false; - }); + #region 按钮状态 + // 假设初始状态所有计划的按钮状态 + list.ForEach(plan => + { + plan.StartEnable = true; + plan.StopEnable = false; + }); - // 查找是否存在正在执行的计划 - var executingPlan = list.FirstOrDefault(x => x.PlanStatus == PlanStatusEnum.已开始); - if (executingPlan != null) - { - // 如果有正在执行的计划,只有该计划的暂停按钮可以点击,其他计划的按钮都不能点击 - foreach (var plan in list) + // 查找是否存在正在执行的计划 + var executingPlan = list.FirstOrDefault(x => x.PlanStatus == PlanStatusEnum.已开始); + if (executingPlan != null) { - if (plan == executingPlan) + // 如果有正在执行的计划,只有该计划的暂停按钮可以点击,其他计划的按钮都不能点击 + foreach (var plan in list) { - plan.StopEnable = true; // 执行中的计划可以暂停 - plan.StartEnable = false; - } - else - { - plan.StartEnable = false; // 其他计划不能开始 + if (plan == executingPlan) + { + plan.StopEnable = true; // 执行中的计划可以暂停 + plan.StartEnable = false; + } + else + { + plan.StartEnable = false; // 其他计划不能开始 + } } } - } - #endregion - + #endregion + list.OrderByDescending(x => x.PlanStatus); + list.ForEach( + arg => + { + PlanInfoDataGrid.Add(arg); + }); - list.OrderByDescending(x => x.PlanStatus); - list.ForEach( - arg => - { - PlanInfoDataGrid.Add(arg); - }); + var info = list.Where(x => x.PlanStatus == PlanStatusEnum.已开始).ToList(); + if (info.Count > 0) + { + RefreshPlanExec(info.First()); + } - var info = list.Where(x => x.PlanStatus == PlanStatusEnum.已开始).ToList(); - if (info.Count > 0) - { - RefreshPlanExec(info.First()); } - - } + })); + } catch (Exception ex) {