BaseApplication.cs 2.0 KB

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