BaseApplication.cs 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. using System;
  2. using System.Windows;
  3. using MediaBrowser.Common.Kernel;
  4. using MediaBrowser.Common.Logging;
  5. using MediaBrowser.Model.Progress;
  6. namespace MediaBrowser.Common.UI
  7. {
  8. /// <summary>
  9. /// Serves as a base Application class for both the UI and Server apps.
  10. /// </summary>
  11. public abstract class BaseApplication : Application
  12. {
  13. private IKernel Kernel { get; set; }
  14. protected abstract IKernel InstantiateKernel();
  15. protected abstract Window InstantiateMainWindow();
  16. protected override void OnStartup(StartupEventArgs e)
  17. {
  18. // Without this the app will shutdown after the splash screen closes
  19. ShutdownMode = ShutdownMode.OnExplicitShutdown;
  20. LoadKernel();
  21. }
  22. private async void LoadKernel()
  23. {
  24. Kernel = InstantiateKernel();
  25. var progress = new Progress<TaskProgress>();
  26. var splash = new Splash(progress);
  27. splash.Show();
  28. try
  29. {
  30. DateTime now = DateTime.UtcNow;
  31. await Kernel.Init(progress);
  32. Logger.LogInfo("Kernel.Init completed in {0} seconds.", (DateTime.UtcNow - now).TotalSeconds);
  33. splash.Close();
  34. ShutdownMode = System.Windows.ShutdownMode.OnLastWindowClose;
  35. InstantiateMainWindow().ShowDialog();
  36. }
  37. catch (Exception ex)
  38. {
  39. if (Logger.LoggerInstance != null)
  40. {
  41. Logger.LogException(ex);
  42. }
  43. MessageBox.Show("There was an error launching Media Browser: " + ex.Message);
  44. splash.Close();
  45. // Shutdown the app with an error code
  46. Shutdown(1);
  47. }
  48. }
  49. protected override void OnExit(ExitEventArgs e)
  50. {
  51. base.OnExit(e);
  52. Kernel.Dispose();
  53. }
  54. }
  55. }