Files
XLAB/XLAB2/App.xaml.cs
Курнат Андрей db15503315 edit
2026-04-01 19:33:59 +03:00

157 lines
5.0 KiB
C#

using System;
using System.Globalization;
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;
namespace XLAB2
{
public partial class App : Application
{
private IHost _host;
public App()
{
ApplyRussianCulture();
RegisterGlobalExceptionHandlers();
}
protected override async void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
try
{
_host = AppHost.Create();
await _host.StartAsync().ConfigureAwait(true);
MainWindow = _host.Services.GetRequiredService<MainWindow>();
MainWindow.Show();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "XLAB2", MessageBoxButton.OK, MessageBoxImage.Error);
Shutdown(-1);
}
}
protected override async void OnExit(ExitEventArgs e)
{
try
{
if (_host != null)
{
try
{
await _host.StopAsync(TimeSpan.FromSeconds(5)).ConfigureAwait(true);
}
finally
{
_host.Dispose();
}
}
}
catch (Exception ex)
{
ShowUnhandledException(ex, true);
}
finally
{
UnregisterGlobalExceptionHandlers();
base.OnExit(e);
}
}
private static void ApplyRussianCulture()
{
var culture = new CultureInfo("ru-RU");
CultureInfo.DefaultThreadCurrentCulture = culture;
CultureInfo.DefaultThreadCurrentUICulture = culture;
Thread.CurrentThread.CurrentCulture = culture;
Thread.CurrentThread.CurrentUICulture = culture;
FrameworkElement.LanguageProperty.OverrideMetadata(
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;
}
}
}