BaseApplication.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. using MediaBrowser.Common.Kernel;
  2. using MediaBrowser.Common.Logging;
  3. using MediaBrowser.Model.Progress;
  4. using Microsoft.Shell;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.ComponentModel;
  8. using System.Windows;
  9. namespace MediaBrowser.Common.UI
  10. {
  11. /// <summary>
  12. /// Serves as a base Application class for both the UI and Server apps.
  13. /// </summary>
  14. public abstract class BaseApplication : Application, INotifyPropertyChanged, ISingleInstanceApp
  15. {
  16. private IKernel Kernel { get; set; }
  17. protected abstract IKernel InstantiateKernel();
  18. protected abstract Window InstantiateMainWindow();
  19. public event PropertyChangedEventHandler PropertyChanged;
  20. public void OnPropertyChanged(String info)
  21. {
  22. if (PropertyChanged != null)
  23. {
  24. PropertyChanged(this, new PropertyChangedEventArgs(info));
  25. }
  26. }
  27. protected override void OnStartup(StartupEventArgs e)
  28. {
  29. // Without this the app will shutdown after the splash screen closes
  30. ShutdownMode = ShutdownMode.OnExplicitShutdown;
  31. LoadKernel();
  32. }
  33. private async void LoadKernel()
  34. {
  35. Kernel = InstantiateKernel();
  36. var progress = new Progress<TaskProgress>();
  37. var splash = new Splash(progress);
  38. splash.Show();
  39. try
  40. {
  41. DateTime now = DateTime.UtcNow;
  42. await Kernel.Init(progress);
  43. Logger.LogInfo("Kernel.Init completed in {0} seconds.", (DateTime.UtcNow - now).TotalSeconds);
  44. splash.Close();
  45. ShutdownMode = System.Windows.ShutdownMode.OnLastWindowClose;
  46. OnKernelLoaded();
  47. InstantiateMainWindow().Show();
  48. }
  49. catch (Exception ex)
  50. {
  51. if (Logger.LoggerInstance != null)
  52. {
  53. Logger.LogException(ex);
  54. }
  55. MessageBox.Show("There was an error launching Media Browser: " + ex.Message);
  56. splash.Close();
  57. // Shutdown the app with an error code
  58. Shutdown(1);
  59. }
  60. }
  61. protected virtual void OnKernelLoaded()
  62. {
  63. }
  64. protected override void OnExit(ExitEventArgs e)
  65. {
  66. base.OnExit(e);
  67. Kernel.Dispose();
  68. }
  69. public bool SignalExternalCommandLineArgs(IList<string> args)
  70. {
  71. OnSecondInstanceLaunched(args);
  72. return true;
  73. }
  74. protected virtual void OnSecondInstanceLaunched(IList<string> args)
  75. {
  76. if (this.MainWindow.WindowState == WindowState.Minimized)
  77. {
  78. this.MainWindow.WindowState = WindowState.Maximized;
  79. }
  80. }
  81. public static void RunApplication<TApplicationType>(string uniqueKey)
  82. where TApplicationType : BaseApplication, IApplication, new()
  83. {
  84. if (SingleInstance<TApplicationType>.InitializeAsFirstInstance(uniqueKey))
  85. {
  86. var application = new TApplicationType();
  87. application.InitializeComponent();
  88. application.Run();
  89. // Allow single instance code to perform cleanup operations
  90. SingleInstance<TApplicationType>.Cleanup();
  91. }
  92. }
  93. }
  94. public interface IApplication
  95. {
  96. void InitializeComponent();
  97. }
  98. }