98 lines
3.0 KiB
C#
98 lines
3.0 KiB
C#
using System;
|
||
using System.IO;
|
||
using System.Threading.Tasks;
|
||
using System.Windows;
|
||
using System.Windows.Threading;
|
||
using SiteViewer.Services;
|
||
|
||
namespace SiteViewer
|
||
{
|
||
public partial class App : Application
|
||
{
|
||
public static SitesStore Sites { get; } = new SitesStore();
|
||
public static UiSettings Ui { get; } = new UiSettings();
|
||
public static AdminHost Admin { get; } = new AdminHost(Sites);
|
||
|
||
public static string WebViewUserDataFolder { get; } = Path.Combine(
|
||
Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),
|
||
"SiteViewer",
|
||
"WebView2");
|
||
|
||
protected override void OnStartup(StartupEventArgs e)
|
||
{
|
||
DispatcherUnhandledException += (_, args) =>
|
||
{
|
||
args.Handled = true;
|
||
try
|
||
{
|
||
MessageBox.Show(
|
||
"Произошла ошибка, приложение продолжит работу.\n\n" + args.Exception.Message,
|
||
"SiteViewer",
|
||
MessageBoxButton.OK,
|
||
MessageBoxImage.Warning);
|
||
}
|
||
catch
|
||
{
|
||
// ignore
|
||
}
|
||
};
|
||
|
||
AppDomain.CurrentDomain.UnhandledException += (_, args) =>
|
||
{
|
||
if (args.ExceptionObject is Exception ex)
|
||
{
|
||
try
|
||
{
|
||
var folder = Path.Combine(
|
||
Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),
|
||
"SiteViewer");
|
||
Directory.CreateDirectory(folder);
|
||
File.AppendAllText(
|
||
Path.Combine(folder, "crash.log"),
|
||
"[" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "] " + ex + "\n\n");
|
||
}
|
||
catch
|
||
{
|
||
// ignore
|
||
}
|
||
}
|
||
};
|
||
|
||
TaskScheduler.UnobservedTaskException += (_, args) =>
|
||
{
|
||
args.SetObserved();
|
||
};
|
||
|
||
Directory.CreateDirectory(WebViewUserDataFolder);
|
||
base.OnStartup(e);
|
||
|
||
try
|
||
{
|
||
Admin.Start();
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
MessageBox.Show(
|
||
"Не удалось запустить веб-сервер на порту " + AdminHost.Port + ".\n" + ex.Message,
|
||
"SiteViewer",
|
||
MessageBoxButton.OK,
|
||
MessageBoxImage.Warning);
|
||
}
|
||
}
|
||
|
||
protected override void OnExit(ExitEventArgs e)
|
||
{
|
||
try
|
||
{
|
||
Admin.Dispose();
|
||
}
|
||
catch
|
||
{
|
||
// ignore
|
||
}
|
||
|
||
base.OnExit(e);
|
||
}
|
||
}
|
||
}
|