Exemplo n.º 1
0
    private static void Main(string[] args)
    {
      if (ProcessHelper.IsProcessAlreadyRunning())
        return;

      Thread.CurrentThread.Name = "Main Thread";
      IrssLog.Open("IR Server.log");

      try
      {
        if (args.Length == 0)
        {
          IRServer = new IRServer();
          if (IRServer.DoStart())
          {
            SystemEvents.PowerModeChanged += new PowerModeChangedEventHandler(OnPowerModeChanged);

            ReceiverWindow receiverWindow = new ReceiverWindow(Shared.ServerWindowName);

            Application.Run();
            SystemEvents.PowerModeChanged -= new PowerModeChangedEventHandler(OnPowerModeChanged);

            receiverWindow.DestroyHandle();
            receiverWindow = null;
            IRServer.DoStop();
          }
        }
        else
        {
          foreach (string parameter in args)
          {
            switch (parameter.ToUpperInvariant().Replace("-", "/"))
            {
              case "/INSTALL":
                IrssLog.Info("Installing IR Server ...");
                using (TransactedInstaller transactedInstaller = new TransactedInstaller())
                {
                  using (IRServerInstaller IRServerInstaller = new IRServerInstaller())
                  {
                    transactedInstaller.Installers.Add(IRServerInstaller);

                    string path = "/assemblypath=" + Assembly.GetExecutingAssembly().Location;
                    string[] cmdline = { path };

                    InstallContext installContext = new InstallContext(String.Empty, cmdline);
                    transactedInstaller.Context = installContext;

                    transactedInstaller.Install(new Hashtable());
                  }
                }
                break;

              case "/UNINSTALL":
                IrssLog.Info("Uninstalling IR Server ...");
                using (TransactedInstaller transactedInstaller = new TransactedInstaller())
                {
                  using (IRServerInstaller IRServerInstaller = new IRServerInstaller())
                  {
                    transactedInstaller.Installers.Add(IRServerInstaller);

                    string path = "/assemblypath=" + Assembly.GetExecutingAssembly().Location;
                    string[] cmdline = { path };

                    InstallContext installContext = new InstallContext(String.Empty, cmdline);
                    transactedInstaller.Context = installContext;

                    transactedInstaller.Uninstall(null);
                  }
                }
                break;

              case "/START":
                IrssLog.Info("Starting IR Server ...");
                using (ServiceController serviceController = new ServiceController(Shared.ServerName))
                  if (serviceController.Status == ServiceControllerStatus.Stopped)
                    serviceController.Start();
                break;

              case "/STOP":
                IrssLog.Info("Stopping IR Server ...");
                using (ServiceController serviceController = new ServiceController(Shared.ServerName))
                  if (serviceController.Status == ServiceControllerStatus.Running)
                    serviceController.Stop();
                break;

              case "/RESTART":
                IrssLog.Info("Restarting IR Server ...");
                using (ServiceController serviceController = new ServiceController(Shared.ServerName))
                {
                  if (serviceController.Status == ServiceControllerStatus.Running)
                    serviceController.Stop();

                  serviceController.WaitForStatus(ServiceControllerStatus.Stopped, new TimeSpan(0, 0, 30));

                  if (serviceController.Status == ServiceControllerStatus.Stopped)
                    serviceController.Start();
                }
                break;

              case "/SERVICE":
                {
                  IRServer IRServer = new IRServer();
                  ServiceBase.Run(IRServer);
                }
                break;

              default:
                throw new InvalidOperationException(String.Format("Unknown command line parameter \"{0}\"", parameter));
            }
          }

          IrssLog.Info("Done.");
        }
      }
      catch (Exception ex)
      {
        IrssLog.Error(ex);
      }
      finally
      {
        IrssLog.Close();
      }
    }
Exemplo n.º 2
0
        private static void Main(string[] args)
        {
            if (ProcessHelper.IsProcessAlreadyRunning())
            {
                return;
            }

            Thread.CurrentThread.Name = "Main Thread";
            IrssLog.Open("IR Server.log");

            try
            {
                if (args.Length == 0)
                {
                    IRServer = new IRServer();
                    if (IRServer.DoStart())
                    {
                        SystemEvents.PowerModeChanged += new PowerModeChangedEventHandler(OnPowerModeChanged);

                        ReceiverWindow receiverWindow = new ReceiverWindow(Shared.ServerWindowName);

                        Application.Run();
                        SystemEvents.PowerModeChanged -= new PowerModeChangedEventHandler(OnPowerModeChanged);

                        receiverWindow.DestroyHandle();
                        receiverWindow = null;
                        IRServer.DoStop();
                    }
                }
                else
                {
                    foreach (string parameter in args)
                    {
                        switch (parameter.ToUpperInvariant().Replace("-", "/"))
                        {
                        case "/INSTALL":
                            IrssLog.Info("Installing IR Server ...");
                            using (TransactedInstaller transactedInstaller = new TransactedInstaller())
                            {
                                using (IRServerInstaller IRServerInstaller = new IRServerInstaller())
                                {
                                    transactedInstaller.Installers.Add(IRServerInstaller);

                                    string   path    = "/assemblypath=" + Assembly.GetExecutingAssembly().Location;
                                    string[] cmdline = { path };

                                    InstallContext installContext = new InstallContext(String.Empty, cmdline);
                                    transactedInstaller.Context = installContext;

                                    transactedInstaller.Install(new Hashtable());
                                }
                            }
                            break;

                        case "/UNINSTALL":
                            IrssLog.Info("Uninstalling IR Server ...");
                            using (TransactedInstaller transactedInstaller = new TransactedInstaller())
                            {
                                using (IRServerInstaller IRServerInstaller = new IRServerInstaller())
                                {
                                    transactedInstaller.Installers.Add(IRServerInstaller);

                                    string   path    = "/assemblypath=" + Assembly.GetExecutingAssembly().Location;
                                    string[] cmdline = { path };

                                    InstallContext installContext = new InstallContext(String.Empty, cmdline);
                                    transactedInstaller.Context = installContext;

                                    transactedInstaller.Uninstall(null);
                                }
                            }
                            break;

                        case "/START":
                            IrssLog.Info("Starting IR Server ...");
                            using (ServiceController serviceController = new ServiceController(Shared.ServerName))
                                if (serviceController.Status == ServiceControllerStatus.Stopped)
                                {
                                    serviceController.Start();
                                }
                            break;

                        case "/STOP":
                            IrssLog.Info("Stopping IR Server ...");
                            using (ServiceController serviceController = new ServiceController(Shared.ServerName))
                                if (serviceController.Status == ServiceControllerStatus.Running)
                                {
                                    serviceController.Stop();
                                }
                            break;

                        case "/RESTART":
                            IrssLog.Info("Restarting IR Server ...");
                            using (ServiceController serviceController = new ServiceController(Shared.ServerName))
                            {
                                if (serviceController.Status == ServiceControllerStatus.Running)
                                {
                                    serviceController.Stop();
                                }

                                serviceController.WaitForStatus(ServiceControllerStatus.Stopped, new TimeSpan(0, 0, 30));

                                if (serviceController.Status == ServiceControllerStatus.Stopped)
                                {
                                    serviceController.Start();
                                }
                            }
                            break;

                        case "/SERVICE":
                        {
                            IRServer IRServer = new IRServer();
                            ServiceBase.Run(IRServer);
                        }
                        break;

                        default:
                            throw new InvalidOperationException(String.Format("Unknown command line parameter \"{0}\"", parameter));
                        }
                    }

                    IrssLog.Info("Done.");
                }
            }
            catch (Exception ex)
            {
                IrssLog.Error(ex);
            }
            finally
            {
                IrssLog.Close();
            }
        }