aboutsummaryrefslogtreecommitdiff
path: root/MediaBrowser.ServerApplication/App.xaml.cs
diff options
context:
space:
mode:
authorLuke Pulverenti <luke.pulverenti@gmail.com>2013-09-20 21:04:14 -0400
committerLuke Pulverenti <luke.pulverenti@gmail.com>2013-09-20 21:04:14 -0400
commit2e511fba839e86d9393e5eeb10795f1b0aed7ce0 (patch)
treed82481b8352ae2f088cbe902cc7fc222b8b0a0ed /MediaBrowser.ServerApplication/App.xaml.cs
parentb5615cb233923f8424a40af009101a901f30f591 (diff)
support run as service
Diffstat (limited to 'MediaBrowser.ServerApplication/App.xaml.cs')
-rw-r--r--MediaBrowser.ServerApplication/App.xaml.cs116
1 files changed, 68 insertions, 48 deletions
diff --git a/MediaBrowser.ServerApplication/App.xaml.cs b/MediaBrowser.ServerApplication/App.xaml.cs
index 42045257a..69de391a4 100644
--- a/MediaBrowser.ServerApplication/App.xaml.cs
+++ b/MediaBrowser.ServerApplication/App.xaml.cs
@@ -1,4 +1,5 @@
-using MediaBrowser.Controller;
+using MediaBrowser.Common.Events;
+using MediaBrowser.Controller;
using MediaBrowser.Controller.Configuration;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Model.Logging;
@@ -12,32 +13,35 @@ namespace MediaBrowser.ServerApplication
/// <summary>
/// Interaction logic for App.xaml
/// </summary>
- public partial class App : Application, IApplicationInterface
+ public partial class App : Application
{
/// <summary>
/// Gets or sets the logger.
/// </summary>
/// <value>The logger.</value>
- protected ILogger Logger { get; set; }
+ private readonly ILogger _logger;
/// <summary>
/// Gets or sets the composition root.
/// </summary>
/// <value>The composition root.</value>
- protected ApplicationHost CompositionRoot { get; set; }
+ private readonly ApplicationHost _appHost;
+
+ public event EventHandler AppStarted;
+
+ public bool IsRunningAsService { get; private set; }
/// <summary>
/// Initializes a new instance of the <see cref="App" /> class.
/// </summary>
/// <param name="logger">The logger.</param>
- public App()
+ public App(ApplicationHost appHost, ILogger logger, bool isRunningAsService)
{
- InitializeComponent();
- }
+ _appHost = appHost;
+ _logger = logger;
+ IsRunningAsService = isRunningAsService;
- public bool IsBackgroundService
- {
- get { return false; }
+ InitializeComponent();
}
/// <summary>
@@ -51,7 +55,7 @@ namespace MediaBrowser.ServerApplication
public void OnUnhandledException(Exception ex)
{
- Logger.ErrorException("UnhandledException", ex);
+ _logger.ErrorException("UnhandledException", ex);
MessageBox.Show("Unhandled exception: " + ex.Message);
}
@@ -70,27 +74,32 @@ namespace MediaBrowser.ServerApplication
{
try
{
- CompositionRoot = new ApplicationHost(this);
-
- Logger = CompositionRoot.LogManager.GetLogger("App");
-
- var splash = new SplashWindow(CompositionRoot.ApplicationVersion);
+ if (!IsRunningAsService)
+ {
+ ShowSplashWindow();
+ }
- splash.Show();
+ await _appHost.Init();
- await CompositionRoot.Init();
+ if (!IsRunningAsService)
+ {
+ HideSplashWindow();
+ }
- splash.Hide();
+ var task = _appHost.RunStartupTasks();
- var task = CompositionRoot.RunStartupTasks();
+ if (!IsRunningAsService)
+ {
+ ShowMainWindow();
+ }
- new MainWindow(CompositionRoot.LogManager, CompositionRoot, CompositionRoot.ServerConfigurationManager, CompositionRoot.UserManager, CompositionRoot.LibraryManager, CompositionRoot.JsonSerializer, CompositionRoot.DisplayPreferencesRepository).Show();
+ EventHelper.FireEventIfNotNull(AppStarted, this, EventArgs.Empty, _logger);
await task.ConfigureAwait(false);
}
catch (Exception ex)
{
- Logger.ErrorException("Error launching application", ex);
+ _logger.ErrorException("Error launching application", ex);
MessageBox.Show("There was an error launching Media Browser: " + ex.Message);
@@ -99,27 +108,53 @@ namespace MediaBrowser.ServerApplication
}
}
- public void ShutdownApplication()
+ private MainWindow _mainWindow;
+ private void ShowMainWindow()
{
- Dispatcher.Invoke(Shutdown);
+ var host = _appHost;
+
+ var win = new MainWindow(host.LogManager, host,
+ host.ServerConfigurationManager, host.UserManager,
+ host.LibraryManager, host.JsonSerializer,
+ host.DisplayPreferencesRepository);
+
+ win.Show();
+
+ _mainWindow = win;
}
- /// <summary>
- /// Raises the <see cref="E:System.Windows.Application.Exit" /> event.
- /// </summary>
- /// <param name="e">An <see cref="T:System.Windows.ExitEventArgs" /> that contains the event data.</param>
- protected override void OnExit(ExitEventArgs e)
+ private void HideMainWindow()
{
- MainStartup.ReleaseMutex();
+ if (_mainWindow != null)
+ {
+ _mainWindow.Hide();
+ _mainWindow = null;
+ }
+ }
- base.OnExit(e);
+ private SplashWindow _splashWindow;
+ private void ShowSplashWindow()
+ {
+ var win = new SplashWindow(_appHost.ApplicationVersion);
+ win.Show();
- if (CompositionRoot != null)
+ _splashWindow = win;
+ }
+
+ private void HideSplashWindow()
+ {
+ if (_splashWindow != null)
{
- CompositionRoot.Dispose();
+ _splashWindow.Hide();
+ _splashWindow = null;
}
}
+ public void ShutdownApplication()
+ {
+ Dispatcher.Invoke(Shutdown);
+ }
+
/// <summary>
/// Opens the dashboard page.
/// </summary>
@@ -172,20 +207,5 @@ namespace MediaBrowser.ServerApplication
{
((Process)sender).Dispose();
}
-
- /// <summary>
- /// Restarts this instance.
- /// </summary>
- /// <exception cref="System.NotImplementedException"></exception>
- public void RestartApplication()
- {
- Dispatcher.Invoke(MainStartup.ReleaseMutex);
-
- CompositionRoot.Dispose();
-
- System.Windows.Forms.Application.Restart();
-
- Dispatcher.Invoke(Shutdown);
- }
}
}