Exemplo n.º 1
0
        protected override void OnStop()
        {
            try
            {
                //Request additional time
                if (_serviceDefinition != null)
                {
                    RequestAdditionalTime(_serviceDefinition.ProcessTimeoutTime + 1000);
                }

                if (_dmProcess != null)
                {
                    _dmProcess.StopProcess();
                    _dmProcess.Dispose();
                    _dmProcess = null;
                }
            }
            catch (Exception ex)
            {
                Logger.Error(ex, ex.Message);
                Stop();
            }

            base.OnStop();
        }
Exemplo n.º 2
0
        protected override void OnStart(string[] args)
        {
            base.OnStart(args);

            try
            {
                //Get the service name
                _serviceName = GetServiceName();

                //Get data from registry
                _serviceDefinition = RegistryManagement.LoadServiceStartInfosFromRegistry(_serviceName);

                //--------------------------------------------------------------------

                //Setup NLOG for the service
                if (_serviceDefinition.UseEventLog)
                {
                    SetupEventLogService();
                }

                //Request additional time
                RequestAdditionalTime(_serviceDefinition.ProcessTimeoutTime + 1000);

                //Create a new DmProcess instance with reg data
                _dmProcess = new DmProcess(_serviceDefinition);
                _dmProcess.MaxRestartsReached += DmProcessOnMaxRestartsReached;
                _dmProcess.UpdateProcessPid   += DmProcessOnUpdateProcessPid;

                //Check if the service should start in a user session or in the service session
                string sessionUsername = RegistryManagement.ReadAndClearSessionUsername(_serviceName);
                if (string.IsNullOrWhiteSpace(sessionUsername))
                {
                    Logger.Info("Starting the process in service session...");
                    _dmProcess.StartProcess(null);
                }
                else
                {
                    Logger.Info("Starting the process in user session...");
                    _dmProcess.StartProcess(sessionUsername);
                }
            }
            catch (Exception ex)
            {
                Logger.Error(ex, ex.Message);
                Stop();
            }
        }