App.xaml.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. using MediaBrowser.Common.Events;
  2. using MediaBrowser.Controller;
  3. using MediaBrowser.Controller.Configuration;
  4. using MediaBrowser.Controller.Entities;
  5. using MediaBrowser.Model.Logging;
  6. using MediaBrowser.ServerApplication.Splash;
  7. using System;
  8. using System.Diagnostics;
  9. using System.Windows;
  10. namespace MediaBrowser.ServerApplication
  11. {
  12. /// <summary>
  13. /// Interaction logic for App.xaml
  14. /// </summary>
  15. public partial class App : Application
  16. {
  17. /// <summary>
  18. /// Gets or sets the logger.
  19. /// </summary>
  20. /// <value>The logger.</value>
  21. private readonly ILogger _logger;
  22. /// <summary>
  23. /// Gets or sets the composition root.
  24. /// </summary>
  25. /// <value>The composition root.</value>
  26. private readonly ApplicationHost _appHost;
  27. public event EventHandler AppStarted;
  28. public bool IsRunningAsService { get; private set; }
  29. /// <summary>
  30. /// Initializes a new instance of the <see cref="App" /> class.
  31. /// </summary>
  32. /// <param name="logger">The logger.</param>
  33. public App(ApplicationHost appHost, ILogger logger, bool isRunningAsService)
  34. {
  35. _appHost = appHost;
  36. _logger = logger;
  37. IsRunningAsService = isRunningAsService;
  38. InitializeComponent();
  39. }
  40. /// <summary>
  41. /// Gets the name of the uninstaller file.
  42. /// </summary>
  43. /// <value>The name of the uninstaller file.</value>
  44. protected string UninstallerFileName
  45. {
  46. get { return "MediaBrowser.Server.Uninstall.exe"; }
  47. }
  48. public void OnUnhandledException(Exception ex)
  49. {
  50. MessageBox.Show("Unhandled exception: " + ex.Message);
  51. }
  52. protected override void OnStartup(StartupEventArgs e)
  53. {
  54. base.OnStartup(e);
  55. LoadApplication();
  56. }
  57. /// <summary>
  58. /// Loads the kernel.
  59. /// </summary>
  60. protected async void LoadApplication()
  61. {
  62. try
  63. {
  64. if (!IsRunningAsService)
  65. {
  66. ShowSplashWindow();
  67. }
  68. await _appHost.Init();
  69. var task = _appHost.RunStartupTasks();
  70. if (!IsRunningAsService)
  71. {
  72. HideSplashWindow();
  73. }
  74. if (!IsRunningAsService)
  75. {
  76. ShowMainWindow();
  77. }
  78. EventHelper.FireEventIfNotNull(AppStarted, this, EventArgs.Empty, _logger);
  79. await task;
  80. }
  81. catch (Exception ex)
  82. {
  83. _logger.ErrorException("Error launching application", ex);
  84. MessageBox.Show("There was an error launching Media Browser: " + ex.Message);
  85. // Shutdown the app with an error code
  86. Shutdown(1);
  87. }
  88. }
  89. private MainWindow _mainWindow;
  90. private void ShowMainWindow()
  91. {
  92. var host = _appHost;
  93. var win = new MainWindow(host.LogManager, host,
  94. host.ServerConfigurationManager, host.UserManager,
  95. host.LibraryManager, host.JsonSerializer,
  96. host.DisplayPreferencesRepository);
  97. win.Show();
  98. _mainWindow = win;
  99. }
  100. private void HideMainWindow()
  101. {
  102. if (_mainWindow != null)
  103. {
  104. _mainWindow.Hide();
  105. _mainWindow = null;
  106. }
  107. }
  108. private SplashWindow _splashWindow;
  109. private void ShowSplashWindow()
  110. {
  111. var win = new SplashWindow(_appHost.ApplicationVersion);
  112. win.Show();
  113. _splashWindow = win;
  114. }
  115. private void HideSplashWindow()
  116. {
  117. if (_splashWindow != null)
  118. {
  119. _splashWindow.Hide();
  120. _splashWindow = null;
  121. }
  122. }
  123. public void ShutdownApplication()
  124. {
  125. Dispatcher.Invoke(Shutdown);
  126. }
  127. }
  128. }