Ejemplo n.º 1
0
        private void ThreadProc(object args)
        {
            try
            {
                SynchronizationContext.SetSynchronizationContext(this.syncContext);
                using (var container = Program.InitializeContainer(this.syncContext, false))
                {
                    this.log = container.GetInstance <ILogger>();

                    ICarpetProcessor carpetProcessor = container.GetInstance <ICarpetProcessor>();
                    carpetProcessor.Start();
                    this.syncContext.RunOnCurrentThread();
                    container.GetInstance <IProgramState>().Save();
                    carpetProcessor.Stop();
                }
            }
            catch (Exception ex)
            {
                this.log.Error("Service thread error." + Environment.NewLine + ex.ToString());
            }
        }
Ejemplo n.º 2
0
        private static void Main(string[] args)
        {
            AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
            try
            {
                string instanceName = @"Local\{7A97254E-AFC8-4C0C-A6DB-C6DA0BFB463F}";
                using (new SingleInstance(instanceName))
                {
                    Log.Logger = new LoggerConfiguration()
                                 .ReadFrom.AppSettings()
                                 .CreateLogger();

                    if (Environment.UserInteractive)
                    {
                        if (args.Contains("--migrate"))
                        {
                            var runner = new MahloService.DbMigrations.Runner(new DbLocal(new DbConnectionFactory.Factory()));
                            runner.MigrateToLatest();
                            Environment.Exit(0);
                        }

                        if (args.Contains("--install"))
                        {
                            var runner = new MahloService.DbMigrations.Runner(new DbLocal(new DbConnectionFactory.Factory()));
                            runner.MigrateToLatest();
                            ManagedInstallerClass.InstallHelper(new string[] { Assembly.GetExecutingAssembly().Location });
                            new ServiceController(StrMahloMapper).Start();
                            Environment.Exit(0);
                        }

                        if (args.Contains("--uninstall"))
                        {
                            ManagedInstallerClass.InstallHelper(new string[] { "/u", Assembly.GetExecutingAssembly().Location });
                            Environment.Exit(0);
                        }

                        bool shouldSimulate = args.Contains("--simulate");
                        if (shouldSimulate)
                        {
                            Application.EnableVisualStyles();
                            Application.SetCompatibleTextRenderingDefault(false);
                            using (new Control()) { }
                            using (var container = InitializeContainer(WindowsFormsSynchronizationContext.Current, shouldSimulate))
                            {
                                ICarpetProcessor carpetProcessor = container.GetInstance <ICarpetProcessor>();
                                carpetProcessor.Start();
                                Application.Run(container.GetInstance <FormSim>());
                                container.GetInstance <IProgramState>().Save();
                                carpetProcessor.Stop();
                            }
                        }
                        else
                        {
                            ApplicationContext appContext = new ApplicationContext();
                            WindowsFormsSynchronizationContext.AutoInstall = false;

                            WindowsFormsSynchronizationContext syncContext = new WindowsFormsSynchronizationContext();
                            SynchronizationContext.SetSynchronizationContext(syncContext);

                            using (var consoleCtrl = new ConsoleCtrl())
                                using (var container = InitializeContainer(syncContext, shouldSimulate))
                                {
                                    consoleCtrl.ControlEvent += (sender, e) =>
                                    {
                                        Application.Exit();
                                        e.Result = true;
                                    };

                                    Log.Logger.Information("Application started");
                                    ICarpetProcessor carpetProcessor = container.GetInstance <ICarpetProcessor>();
                                    carpetProcessor.Start();
                                    Application.Run(appContext);
                                    container.GetInstance <IProgramState>().Save();
                                    carpetProcessor.Stop();
                                    Log.Logger.Information("Application stopped");
                                }
                        }
                    }
                    else
                    {
                        service = new Service();
                        ServiceBase.Run(service);
                    }
                }
            }
            catch (SingleInstanceException)
            {
                Environment.Exit(1);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString(), Application.ProductName);
                Environment.Exit(1);
            }
        }