Beispiel #1
0
    /// <summary>
    /// Either shows the application's main window or inits the application in the system tray.
    /// </summary>
    private void OnStartup(object sender, StartupEventArgs args)
    {
      Thread.CurrentThread.Name = "Main";
      Thread.CurrentThread.SetApartmentState(ApartmentState.STA);

      // Parse command line options
      var mpOptions = new CommandLineOptions();
      var parser = new CommandLine.Parser(with => with.HelpWriter = Console.Out);
      parser.ParseArgumentsStrict(args.Args, mpOptions, () => Environment.Exit(1));

      // Check if another instance is already running
      // If new instance was created by UacHelper previous one, assume that previous one is already closed.
      if (SingleInstanceHelper.IsAlreadyRunning(MUTEX_ID, out _mutex))
      {
        _mutex = null;
        // Set focus on previously running app
        SingleInstanceHelper.SwitchToCurrentInstance(SingleInstanceHelper.SHOW_MP2_SERVICEMONITOR_MESSAGE );
        // Stop current instance
        Console.Out.WriteLine("Application already running.");
        Environment.Exit(2);
      }

      // Make sure we're properly handling exceptions
      DispatcherUnhandledException += OnUnhandledException;
      AppDomain.CurrentDomain.UnhandledException += LauncherExceptionHandling.CurrentDomain_UnhandledException;

      var systemStateService = new SystemStateService();
      ServiceRegistration.Set<ISystemStateService>(systemStateService);
      systemStateService.SwitchSystemState(SystemState.Initializing, false);

#if !DEBUG
      string logPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData), @"Team MediaPortal\MP2-ServiceMonitor\Log");
#endif

      try
      {
        ILogger logger = null;
        try
        {
          ApplicationCore.RegisterVitalCoreServices();
          ApplicationCore.RegisterCoreServices();
          logger = ServiceRegistration.Get<ILogger>();

          logger.Debug("Starting Localization");
          Localization localization = new Localization();
          ServiceRegistration.Set<ILocalization>(localization);
          localization.Startup();

          //ApplicationCore.StartCoreServices();

          logger.Debug("UiExtension: Registering ISystemResolver service");
          ServiceRegistration.Set<ISystemResolver>(new SystemResolver());

          logger.Debug("UiExtension: Registering IServerConnectionManager service");
          ServiceRegistration.Set<IServerConnectionManager>(new ServerConnectionManager());

#if !DEBUG
          logPath = ServiceRegistration.Get<IPathManager>().GetPath("<LOG>");
#endif
        }
        catch (Exception e)
        {
          if (logger != null)
            logger.Critical("Error starting application", e);

          systemStateService.SwitchSystemState(SystemState.ShuttingDown, true);
          ServiceRegistration.IsShuttingDown = true;

          ApplicationCore.DisposeCoreServices();

          throw;
        }

        var appController = new AppController();
        ServiceRegistration.Set<IAppController>(appController);

        // Start the application
        logger.Debug("Starting application");
        try
        {
          ServiceRegistration.Get<IServerConnectionManager>().Startup();
          appController.StartUp(mpOptions);
        }
        catch (Exception e)
        {
          logger.Critical("Error executing application", e);
          systemStateService.SwitchSystemState(SystemState.ShuttingDown, true);
          ServiceRegistration.IsShuttingDown = true;
        }
      }
      catch (Exception ex)
      {
#if DEBUG
        var log = new ConsoleLogger(LogLevel.All, false);
        log.Error(ex);
#else
        ServerCrashLogger crash = new ServerCrashLogger(logPath);
        crash.CreateLog(ex);
#endif
        systemStateService.SwitchSystemState(SystemState.Ending, false);
        Current.Shutdown();
      }
    }
Beispiel #2
0
        /// <summary>
        /// Either shows the application's main window or inits the application in the system tray.
        /// </summary>
        private void OnStartup(object sender, StartupEventArgs args)
        {
            Thread.CurrentThread.Name = "Main";
            Thread.CurrentThread.SetApartmentState(ApartmentState.STA);

            // Parse command line options
            var mpOptions = new CommandLineOptions();
            var parser    = new CommandLine.Parser(with => with.HelpWriter = Console.Out);

            parser.ParseArgumentsStrict(args.Args, mpOptions, () => Environment.Exit(1));

            // Check if another instance is already running
            // If new instance was created by UacHelper previous one, assume that previous one is already closed.
            if (SingleInstanceHelper.IsAlreadyRunning(MUTEX_ID, out _mutex))
            {
                _mutex = null;
                // Set focus on previously running app
                SingleInstanceHelper.SwitchToCurrentInstance(SingleInstanceHelper.SHOW_MP2_SERVICEMONITOR_MESSAGE);
                // Stop current instance
                Console.Out.WriteLine("Application already running.");
                Environment.Exit(2);
            }

            // Make sure we're properly handling exceptions
            DispatcherUnhandledException += OnUnhandledException;
            AppDomain.CurrentDomain.UnhandledException += LauncherExceptionHandling.CurrentDomain_UnhandledException;

            var systemStateService = new SystemStateService();

            ServiceRegistration.Set <ISystemStateService>(systemStateService);
            systemStateService.SwitchSystemState(SystemState.Initializing, false);

#if !DEBUG
            string logPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData), @"Team MediaPortal\MP2-ServiceMonitor\Log");
#endif

            try
            {
                ILogger logger = null;
                try
                {
                    ApplicationCore.RegisterVitalCoreServices(true);
                    ApplicationCore.RegisterCoreServices();
                    logger = ServiceRegistration.Get <ILogger>();

                    logger.Debug("Starting Localization");
                    Localization localization = new Localization();
                    ServiceRegistration.Set <ILocalization>(localization);
                    localization.Startup();

                    //ApplicationCore.StartCoreServices();

                    logger.Debug("UiExtension: Registering ISystemResolver service");
                    ServiceRegistration.Set <ISystemResolver>(new SystemResolver());

                    logger.Debug("UiExtension: Registering IServerConnectionManager service");
                    ServiceRegistration.Set <IServerConnectionManager>(new ServerConnectionManager());

#if !DEBUG
                    logPath = ServiceRegistration.Get <IPathManager>().GetPath("<LOG>");
#endif
                }
                catch (Exception e)
                {
                    if (logger != null)
                    {
                        logger.Critical("Error starting application", e);
                    }

                    systemStateService.SwitchSystemState(SystemState.ShuttingDown, true);
                    ServiceRegistration.IsShuttingDown = true;

                    ApplicationCore.DisposeCoreServices();

                    throw;
                }

                InitIpc();

                var appController = new AppController();
                ServiceRegistration.Set <IAppController>(appController);

                // Start the application
                logger.Debug("Starting application");
                try
                {
                    ServiceRegistration.Get <IServerConnectionManager>().Startup();
                    appController.StartUp(mpOptions);
                }
                catch (Exception e)
                {
                    logger.Critical("Error executing application", e);
                    systemStateService.SwitchSystemState(SystemState.ShuttingDown, true);
                    ServiceRegistration.IsShuttingDown = true;
                }
            }
            catch (Exception ex)
            {
#if DEBUG
                var log = new ConsoleLogger(LogLevel.All, false);
                log.Error(ex);
#else
                ServerCrashLogger crash = new ServerCrashLogger(logPath);
                crash.CreateLog(ex);
#endif
                systemStateService.SwitchSystemState(SystemState.Ending, false);
                Current.Shutdown();
            }
        }