BaseApplication.cs 4.9 KB

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