diff --git a/Correlator/App.xaml.cs b/Correlator/App.xaml.cs index ec5c38b..8879a14 100644 --- a/Correlator/App.xaml.cs +++ b/Correlator/App.xaml.cs @@ -1,6 +1,11 @@ -using System.Windows; +using System; +using System.Text; +using System.Threading.Tasks; +using System.Windows; +using System.Windows.Threading; using Correlator.DataService; using Correlator.Dialog; +using Correlator.Util; using Correlator.ViewModels; using Correlator.Views; using Prism.DryIoc; @@ -13,6 +18,55 @@ /// public partial class App : PrismApplication { + /// + /// 全局log捕获 + /// + public App() + { + Startup += delegate + { + //UI线程未捕获异常处理事件 + DispatcherUnhandledException += Dispatcher_UnhandledException; + + //Task线程内未捕获异常处理事件 + TaskScheduler.UnobservedTaskException += TaskScheduler_UnobservedTaskException; + + //非UI线程未捕获异常处理事件 + AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException; + }; + } + + private void Dispatcher_UnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e) + { + "App".WriteLog($"UI线程异常:{e.Exception.Message}"); + } + + private void TaskScheduler_UnobservedTaskException(object sender, UnobservedTaskExceptionEventArgs e) + { + "App".WriteLog($"Task线程异常:{e.Exception.Message}"); + } + + private void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e) + { + var builder = new StringBuilder(); + if (e.IsTerminating) + { + builder.Append("非UI线程发生致命错误"); + } + + builder.Append("非UI线程异常:"); + if (e.ExceptionObject is Exception exception) + { + builder.Append(exception.Message); + } + else + { + builder.Append(e.ExceptionObject); + } + + "App".WriteLog($"{builder}"); + } + protected override Window CreateShell() { return Container.Resolve(); diff --git a/Correlator/App.xaml.cs b/Correlator/App.xaml.cs index ec5c38b..8879a14 100644 --- a/Correlator/App.xaml.cs +++ b/Correlator/App.xaml.cs @@ -1,6 +1,11 @@ -using System.Windows; +using System; +using System.Text; +using System.Threading.Tasks; +using System.Windows; +using System.Windows.Threading; using Correlator.DataService; using Correlator.Dialog; +using Correlator.Util; using Correlator.ViewModels; using Correlator.Views; using Prism.DryIoc; @@ -13,6 +18,55 @@ /// public partial class App : PrismApplication { + /// + /// 全局log捕获 + /// + public App() + { + Startup += delegate + { + //UI线程未捕获异常处理事件 + DispatcherUnhandledException += Dispatcher_UnhandledException; + + //Task线程内未捕获异常处理事件 + TaskScheduler.UnobservedTaskException += TaskScheduler_UnobservedTaskException; + + //非UI线程未捕获异常处理事件 + AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException; + }; + } + + private void Dispatcher_UnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e) + { + "App".WriteLog($"UI线程异常:{e.Exception.Message}"); + } + + private void TaskScheduler_UnobservedTaskException(object sender, UnobservedTaskExceptionEventArgs e) + { + "App".WriteLog($"Task线程异常:{e.Exception.Message}"); + } + + private void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e) + { + var builder = new StringBuilder(); + if (e.IsTerminating) + { + builder.Append("非UI线程发生致命错误"); + } + + builder.Append("非UI线程异常:"); + if (e.ExceptionObject is Exception exception) + { + builder.Append(exception.Message); + } + else + { + builder.Append(e.ExceptionObject); + } + + "App".WriteLog($"{builder}"); + } + protected override Window CreateShell() { return Container.Resolve(); diff --git a/Correlator/ViewModels/MainWindowViewModel.cs b/Correlator/ViewModels/MainWindowViewModel.cs index 61c0f4b..c29f018 100644 --- a/Correlator/ViewModels/MainWindowViewModel.cs +++ b/Correlator/ViewModels/MainWindowViewModel.cs @@ -605,66 +605,88 @@ var dataModel = RuntimeCache.CorrelatorData; if (dataModel.LeftDeviceDataArray != null && dataModel.RightDeviceDataArray != null) { - Console.WriteLine(@"MainWindowViewModel => 开始计算"); - var array = LazyCorrelator.Value.locating( - 11, - (MWNumericArray)dataModel.LeftDeviceDataArray, (MWNumericArray)dataModel.RightDeviceDataArray, - RuntimeCache.AudioSampleRate, - int.Parse(_pipeLength), int.Parse(_soundSpeed), - 0, 0, - 0, 0, - _materialName, - int.Parse(_pipeDiameter), int.Parse(_pipeDiameter), - 1, -1, - -1, -1, - int.Parse(_lowFrequency), int.Parse(_highFrequency) - ); - Console.WriteLine(@"MainWindowViewModel => 计算结束"); - - //数据绑定 - var snr = Convert.ToDouble(array[0].ToString()); //信噪比 - Snr = snr.ToString("0.0") + ":1"; - RedTransmitterDistance = Convert.ToDouble(array[1].ToString()); //距离A - BlueTransmitterDistance = Convert.ToDouble(array[2].ToString()); //距离B - - var maxFreLowOut = Convert.ToInt32(array[6].ToString()); //低频 - var maxFreHighOut = Convert.ToInt32(array[7].ToString()); //高频 - - FilterValue = maxFreLowOut + " ~ " + maxFreHighOut + "Hz"; - - //设置噪声值 - CorrelatorData = dataModel; - - _eventAggregator.GetEvent().Publish(array); - - var time = DateTime.Now.ToString("HHmmss"); - if (RuntimeCache.IsHydrophone) + try { - //保存参数 - var paramConfigFile = $"{_locateDataDir}\\参数配置.{_locateTimes}.{time}.json"; - File.AppendAllText(paramConfigFile, JsonConvert.SerializeObject(_paramConfig)); + Console.WriteLine(@"MainWindowViewModel => 开始计算"); + var array = LazyCorrelator.Value.locating( + 11, + (MWNumericArray)dataModel.LeftDeviceDataArray, (MWNumericArray)dataModel.RightDeviceDataArray, + RuntimeCache.AudioSampleRate, + int.Parse(_pipeLength), int.Parse(_soundSpeed), + 0, 0, + 0, 0, + _materialName, + int.Parse(_pipeDiameter), int.Parse(_pipeDiameter), + 1, -1, + -1, -1, + int.Parse(_lowFrequency), int.Parse(_highFrequency) + ); + Console.WriteLine(@"MainWindowViewModel => 计算结束"); + DialogHub.Get.DismissLoadingDialog(); + + //数据绑定 + var snr = Convert.ToDouble(array[0].ToString()); //信噪比 + Snr = snr.ToString("0.0") + ":1"; + RedTransmitterDistance = Convert.ToDouble(array[1].ToString()); //距离A + BlueTransmitterDistance = Convert.ToDouble(array[2].ToString()); //距离B + + var maxFreLowOut = Convert.ToInt32(array[6].ToString()); //低频 + var maxFreHighOut = Convert.ToInt32(array[7].ToString()); //高频 + + FilterValue = maxFreLowOut + " ~ " + maxFreHighOut + "Hz"; + + //设置噪声值 + CorrelatorData = dataModel; + + _eventAggregator.GetEvent().Publish(array); + + var time = DateTime.Now.ToString("HHmmss"); + if (RuntimeCache.IsHydrophone) + { + //保存参数 + var paramConfigFile = $"{_locateDataDir}\\参数配置.{_locateTimes}.{time}.json"; + File.AppendAllText(paramConfigFile, JsonConvert.SerializeObject(_paramConfig)); + } + + //保存数据 + var fileName = $"{_locateDataDir}\\测试数据.{_locateTimes}.{time}.txt"; + fileName.SaveLocateData(dataModel); + "MainWindowViewModel".WriteLog("定位数据路径:" + fileName); + + //保存了数据之后3s再截图 + _snapShotPath = $"{_locateDataDir}\\快照.{_locateTimes}.{time}.png"; + _snapShotTimer.Start(); + + _runningTimer.Stop(); + StartButtonEnabled = true; + ResetCache(); } + catch (Exception) + { + _runningTimer.Stop(); + DialogHub.Get.DismissLoadingDialog(); - //保存数据 - var fileName = $"{_locateDataDir}\\测试数据.{_locateTimes}.{time}.txt"; - fileName.SaveLocateData(dataModel); - "MainWindowViewModel".WriteLog("定位数据路径:" + fileName); + Application.Current.Dispatcher.Invoke(delegate + { + _dialogService.ShowDialog( + "AlertControlDialog", + new DialogParameters + { + { "AlertType", AlertType.Question }, { "Title", "温馨提示" }, { "Message", "计算出现错误,请重新计算" } + }, + delegate(IDialogResult dialogResult) + { + if (dialogResult.Result == ButtonResult.Cancel) + { + return; + } - //保存了数据之后3s再截图 - _snapShotPath = $"{_locateDataDir}\\快照.{_locateTimes}.{time}.png"; - _snapShotTimer.Start(); - - _runningTimer.Stop(); - StartButtonEnabled = true; - //清空缓存 - RuntimeCache.RedSensorOriginalResp.Clear(); - RuntimeCache.BlueSensorOriginalResp.Clear(); - RuntimeCache.RedSensorResponseTags.Clear(); - RuntimeCache.BlueSensorResponseTags.Clear(); - RuntimeCache.RedSensorIsEnable = false; - RuntimeCache.BlueSensorIsEnable = false; - - DialogHub.Get.DismissLoadingDialog(); + StartButtonEnabled = true; + ResetCache(); + } + ); + }); + } } } @@ -927,7 +949,7 @@ return; } - _dialogService.ShowDialog("NoiseWaveView", null, delegate { }); + _dialogService.Show("NoiseWaveView", null, delegate { }); }); //打开数字键盘 @@ -1050,6 +1072,19 @@ } /// + /// 重置上次计算的缓存 + /// + private void ResetCache() + { + RuntimeCache.RedSensorOriginalResp.Clear(); + RuntimeCache.BlueSensorOriginalResp.Clear(); + RuntimeCache.RedSensorResponseTags.Clear(); + RuntimeCache.BlueSensorResponseTags.Clear(); + RuntimeCache.RedSensorIsEnable = false; + RuntimeCache.BlueSensorIsEnable = false; + } + + /// /// 参数检查 /// ///