change-优化异常处理

dev
liuwf 3 months ago
parent 05657d77ba
commit 1aff8cbc77

@ -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();
}
}

@ -295,9 +295,12 @@ namespace SlnMesnac.WPF.ViewModel
/// 刷新生产计划
/// </summary>
/// <param name="list"></param>
private void RefreshPlanDataGrid(List<MesProductPlanDto> list)
private async void RefreshPlanDataGrid(List<MesProductPlanDto> list)
{
try
{
await App.Current.Dispatcher.BeginInvoke((Action)(() =>
{
PlanInfoDataGrid = new ObservableCollection<MesProductPlanDto>();
if (list != null)
@ -331,8 +334,6 @@ namespace SlnMesnac.WPF.ViewModel
#endregion
list.OrderByDescending(x => x.PlanStatus);
list.ForEach(
arg =>
@ -347,6 +348,8 @@ namespace SlnMesnac.WPF.ViewModel
}
}
}));
}
catch (Exception ex)
{

Loading…
Cancel
Save