154 lines
4.5 KiB
C#
154 lines
4.5 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;
|
|
|
|
namespace CRAWLER;
|
|
|
|
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, "CRAWLER", 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)
|
|
{
|
|
if (e.ExceptionObject is Exception exception)
|
|
{
|
|
ShowUnhandledException(exception, e.IsTerminating);
|
|
return;
|
|
}
|
|
|
|
MessageBox.Show(
|
|
e.ExceptionObject == null ? "Произошла необработанная ошибка." : e.ExceptionObject.ToString(),
|
|
e.IsTerminating ? "CRAWLER - критическая ошибка" : "CRAWLER",
|
|
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 ? "CRAWLER - критическая ошибка" : "CRAWLER",
|
|
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;
|
|
}
|
|
}
|