BaseApplication.cs 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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. public abstract class BaseApplication : Application
  10. {
  11. private IKernel Kernel { get; set; }
  12. protected abstract IKernel InstantiateKernel();
  13. protected abstract Window InstantiateMainWindow();
  14. protected async override void OnStartup(StartupEventArgs e)
  15. {
  16. // Without this the app will shutdown after the splash screen closes
  17. this.ShutdownMode = ShutdownMode.OnExplicitShutdown;
  18. await LoadKernel();
  19. }
  20. private async Task LoadKernel()
  21. {
  22. Kernel = InstantiateKernel();
  23. Progress<TaskProgress> progress = new Progress<TaskProgress>();
  24. Splash splash = new Splash(progress);
  25. splash.Show();
  26. try
  27. {
  28. DateTime now = DateTime.Now;
  29. await Kernel.Init(progress);
  30. double seconds = (DateTime.Now - now).TotalSeconds;
  31. Logger.LogInfo("Kernel.Init completed in {0} seconds.", seconds);
  32. splash.Close();
  33. this.ShutdownMode = System.Windows.ShutdownMode.OnLastWindowClose;
  34. InstantiateMainWindow().ShowDialog();
  35. }
  36. catch (Exception ex)
  37. {
  38. MessageBox.Show("There was an error launching Media Browser: " + ex.Message);
  39. splash.Close();
  40. // Shutdown the app with an error code
  41. Shutdown(1);
  42. }
  43. }
  44. protected override void OnExit(ExitEventArgs e)
  45. {
  46. base.OnExit(e);
  47. Kernel.Dispose();
  48. }
  49. }
  50. }