edit
This commit is contained in:
@@ -4,6 +4,7 @@ using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows;
|
||||
using System.Windows.Markup;
|
||||
using System.Windows.Threading;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using XLAB2.Infrastructure;
|
||||
@@ -17,6 +18,7 @@ namespace XLAB2
|
||||
public App()
|
||||
{
|
||||
ApplyRussianCulture();
|
||||
RegisterGlobalExceptionHandlers();
|
||||
}
|
||||
|
||||
protected override async void OnStartup(StartupEventArgs e)
|
||||
@@ -40,19 +42,29 @@ namespace XLAB2
|
||||
|
||||
protected override async void OnExit(ExitEventArgs e)
|
||||
{
|
||||
if (_host != null)
|
||||
try
|
||||
{
|
||||
try
|
||||
if (_host != null)
|
||||
{
|
||||
await _host.StopAsync(TimeSpan.FromSeconds(5)).ConfigureAwait(true);
|
||||
}
|
||||
finally
|
||||
{
|
||||
_host.Dispose();
|
||||
try
|
||||
{
|
||||
await _host.StopAsync(TimeSpan.FromSeconds(5)).ConfigureAwait(true);
|
||||
}
|
||||
finally
|
||||
{
|
||||
_host.Dispose();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
base.OnExit(e);
|
||||
catch (Exception ex)
|
||||
{
|
||||
ShowUnhandledException(ex, true);
|
||||
}
|
||||
finally
|
||||
{
|
||||
UnregisterGlobalExceptionHandlers();
|
||||
base.OnExit(e);
|
||||
}
|
||||
}
|
||||
|
||||
private static void ApplyRussianCulture()
|
||||
@@ -68,5 +80,77 @@ namespace XLAB2
|
||||
typeof(FrameworkElement),
|
||||
new FrameworkPropertyMetadata(XmlLanguage.GetLanguage(culture.IetfLanguageTag)));
|
||||
}
|
||||
|
||||
private void RegisterGlobalExceptionHandlers()
|
||||
{
|
||||
DispatcherUnhandledException += OnDispatcherUnhandledException;
|
||||
AppDomain.CurrentDomain.UnhandledException += OnCurrentDomainUnhandledException;
|
||||
TaskScheduler.UnobservedTaskException += OnUnobservedTaskException;
|
||||
}
|
||||
|
||||
private void UnregisterGlobalExceptionHandlers()
|
||||
{
|
||||
DispatcherUnhandledException -= OnDispatcherUnhandledException;
|
||||
AppDomain.CurrentDomain.UnhandledException -= OnCurrentDomainUnhandledException;
|
||||
TaskScheduler.UnobservedTaskException -= OnUnobservedTaskException;
|
||||
}
|
||||
|
||||
private void OnDispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e)
|
||||
{
|
||||
ShowUnhandledException(e.Exception, false);
|
||||
e.Handled = true;
|
||||
}
|
||||
|
||||
private void OnCurrentDomainUnhandledException(object sender, UnhandledExceptionEventArgs e)
|
||||
{
|
||||
var exception = e.ExceptionObject as Exception;
|
||||
if (exception != null)
|
||||
{
|
||||
ShowUnhandledException(exception, e.IsTerminating);
|
||||
return;
|
||||
}
|
||||
|
||||
MessageBox.Show(
|
||||
e.ExceptionObject == null ? "Произошла необработанная ошибка." : e.ExceptionObject.ToString(),
|
||||
e.IsTerminating ? "XLAB2 - критическая ошибка" : "XLAB2",
|
||||
MessageBoxButton.OK,
|
||||
MessageBoxImage.Error);
|
||||
}
|
||||
|
||||
private void OnUnobservedTaskException(object sender, UnobservedTaskExceptionEventArgs e)
|
||||
{
|
||||
ShowUnhandledException(e.Exception, false);
|
||||
e.SetObserved();
|
||||
}
|
||||
|
||||
private static void ShowUnhandledException(Exception exception, bool isCritical)
|
||||
{
|
||||
var actualException = UnwrapException(exception);
|
||||
var message = string.IsNullOrWhiteSpace(actualException.Message)
|
||||
? actualException.ToString()
|
||||
: actualException.Message;
|
||||
|
||||
MessageBox.Show(
|
||||
message,
|
||||
isCritical ? "XLAB2 - критическая ошибка" : "XLAB2",
|
||||
MessageBoxButton.OK,
|
||||
MessageBoxImage.Error);
|
||||
}
|
||||
|
||||
private static Exception UnwrapException(Exception exception)
|
||||
{
|
||||
if (exception is AggregateException aggregateException)
|
||||
{
|
||||
var flattened = aggregateException.Flatten();
|
||||
if (flattened.InnerExceptions.Count == 1)
|
||||
{
|
||||
return UnwrapException(flattened.InnerExceptions[0]);
|
||||
}
|
||||
|
||||
return flattened;
|
||||
}
|
||||
|
||||
return exception;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user