From d54c6d8bf63c13c0b47f76fd9f40fec614fbd689 Mon Sep 17 00:00:00 2001 From: LukePulverenti Luke Pulverenti luke pulverenti Date: Sun, 19 Aug 2012 18:47:02 -0400 Subject: Created a BaseApplication class in common --- MediaBrowser.Common/UI/BaseApplication.cs | 62 +++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 MediaBrowser.Common/UI/BaseApplication.cs (limited to 'MediaBrowser.Common/UI/BaseApplication.cs') diff --git a/MediaBrowser.Common/UI/BaseApplication.cs b/MediaBrowser.Common/UI/BaseApplication.cs new file mode 100644 index 000000000..a6bc7f7e4 --- /dev/null +++ b/MediaBrowser.Common/UI/BaseApplication.cs @@ -0,0 +1,62 @@ +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 +{ + 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) + { + this.ShutdownMode = ShutdownMode.OnExplicitShutdown; + + await LoadKernel(); + } + + private async Task LoadKernel() + { + Kernel = InstantiateKernel(); + + Progress progress = new Progress(); + 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(); + Shutdown(1); + } + } + + protected override void OnExit(ExitEventArgs e) + { + base.OnExit(e); + + Kernel.Dispose(); + } + } +} -- cgit v1.2.3