jellyfin/MediaBrowser.Common/UI/BaseApplication.cs

69 lines
2.0 KiB
C#
Raw Normal View History

using System;
using System.Threading.Tasks;
using System.Windows;
using MediaBrowser.Common.Kernel;
using MediaBrowser.Common.Logging;
using MediaBrowser.Model.Progress;
namespace MediaBrowser.Common.UI
{
2012-08-20 00:15:09 +00:00
/// <summary>
/// Serves as a base Application class for both the UI and Server apps.
/// </summary>
public abstract class BaseApplication : Application
{
private IKernel Kernel { get; set; }
protected abstract IKernel InstantiateKernel();
protected abstract Window InstantiateMainWindow();
protected async override void OnStartup(StartupEventArgs e)
{
2012-08-19 22:50:54 +00:00
// Without this the app will shutdown after the splash screen closes
this.ShutdownMode = ShutdownMode.OnExplicitShutdown;
2012-08-20 21:48:11 +00:00
await LoadKernel().ConfigureAwait(false);
}
private async Task LoadKernel()
{
Kernel = InstantiateKernel();
Progress<TaskProgress> progress = new Progress<TaskProgress>();
Splash splash = new Splash(progress);
splash.Show();
try
{
DateTime now = DateTime.Now;
await Kernel.Init(progress);
double seconds = (DateTime.Now - now).TotalSeconds;
Logger.LogInfo("Kernel.Init completed in {0} seconds.", seconds);
splash.Close();
this.ShutdownMode = System.Windows.ShutdownMode.OnLastWindowClose;
InstantiateMainWindow().ShowDialog();
}
catch (Exception ex)
{
MessageBox.Show("There was an error launching Media Browser: " + ex.Message);
splash.Close();
2012-08-19 22:50:54 +00:00
// Shutdown the app with an error code
Shutdown(1);
}
}
protected override void OnExit(ExitEventArgs e)
{
base.OnExit(e);
Kernel.Dispose();
}
}
}