BaseApplication.cs 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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 async override void OnStartup(StartupEventArgs e)
  18. {
  19. // Without this the app will shutdown after the splash screen closes
  20. this.ShutdownMode = ShutdownMode.OnExplicitShutdown;
  21. await LoadKernel();
  22. }
  23. private async Task 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. double seconds = (DateTime.Now - now).TotalSeconds;
  34. Logger.LogInfo("Kernel.Init completed in {0} seconds.", seconds);
  35. splash.Close();
  36. this.ShutdownMode = System.Windows.ShutdownMode.OnLastWindowClose;
  37. InstantiateMainWindow().ShowDialog();
  38. }
  39. catch (Exception ex)
  40. {
  41. MessageBox.Show("There was an error launching Media Browser: " + ex.Message);
  42. splash.Close();
  43. // Shutdown the app with an error code
  44. Shutdown(1);
  45. }
  46. }
  47. protected override void OnExit(ExitEventArgs e)
  48. {
  49. base.OnExit(e);
  50. Kernel.Dispose();
  51. }
  52. }
  53. }