BaseApplication.cs 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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. MessageBox.Show("There was an error launching Media Browser: " + ex.Message);
  41. splash.Close();
  42. // Shutdown the app with an error code
  43. Shutdown(1);
  44. }
  45. }
  46. protected override void OnExit(ExitEventArgs e)
  47. {
  48. base.OnExit(e);
  49. Kernel.Dispose();
  50. }
  51. }
  52. }