BaseApplication.cs 2.1 KB

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