Пример #1
0
        protected void RunAsConsole()
        {
            Application.ThreadException += LauncherExceptionHandling.Application_ThreadException;
            AppDomain.CurrentDomain.UnhandledException += LauncherExceptionHandling.CurrentDomain_UnhandledException;
            TaskScheduler.UnobservedTaskException      += LauncherExceptionHandling.TaskScheduler_UnobservedTaskException;

            Start();

            try
            {
                Application.EnableVisualStyles();
                FormDpiAwarenessExtension.TryEnableDPIAwareness();
                Application.Run(new MainForm());
            }
            catch (Exception e)
            {
                ServiceRegistration.Get <ILogger>().Critical("Error executing application", e);
            }

            Stop();
            Application.ThreadException -= LauncherExceptionHandling.Application_ThreadException;
            AppDomain.CurrentDomain.UnhandledException -= LauncherExceptionHandling.CurrentDomain_UnhandledException;
            TaskScheduler.UnobservedTaskException      -= LauncherExceptionHandling.TaskScheduler_UnobservedTaskException;
        }
Пример #2
0
        /// <summary>
        /// The main entry point for the MP2-ClientLauncher application.
        /// </summary>
        private static void Main(params string[] args)
        {
            Thread.CurrentThread.Name = "Main";

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

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

            // Check if another instance is already running
            if (SingleInstanceHelper.IsAlreadyRunning(MUTEX_ID, out _mutex))
            {
                _mutex = null;
                // Stop current instance
                Console.Out.WriteLine("Application already running.");
                Environment.Exit(2);
            }

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

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

            // Start core services
            ILogger logger = null;
            try
            {
                FormDpiAwarenessExtension.TryEnableDPIAwareness();

                // Check if user wants to override the default Application Data location.
                ApplicationCore.RegisterVitalCoreServices(true, mpOptions.DataDirectory);

                logger = ServiceRegistration.Get <ILogger>();

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

                ApplicationCore.DisposeCoreServices();

                throw;
            }

            // Start application
            logger.Info("Starting application");

            try
            {
                if (TerminateProcess("ehtray"))
                {
                    logger.Info("Terminating running instance(s) of ehtray.exe");
                }

                IsAutoStartEnabled = !string.IsNullOrEmpty(WindowsAPI.GetAutostartApplicationPath(AUTOSTART_REGISTER_NAME, true));

                if (!mpOptions.NoIcon)
                {
                    InitTrayIcon();
                }

                InitIpc();
                InitMsgHandler();

                Application.Run();
            }
            catch (Exception e)
            {
                logger.Critical("Error executing application", e);
            }

            logger.Info("Exiting...");

            CloseMsgHandler();
            CloseIpc();

            // Release mutex for single instance
            if (_mutex != null)
            {
                _mutex.ReleaseMutex();
            }

            Application.Exit();
        }