Example #1
0
        public void ItShallStopImplementationAndReportStopped()
        {
            // Given
            GivenTheServiceHasBeenStarted();

            // When
            sut.OnCommand(ServiceControlCommand.Stop, 0);

            // Then
            A.CallTo(() => serviceImplmentation.Stop()).MustHaveHappened();
            A.CallTo(() => statusReportCallback(ServiceState.Stopped, ServiceAcceptedControlCommandsFlags.None, 0, 0)).MustHaveHappened();
        }
        /// <summary>
        /// Called when a command was received from windows' service system.
        /// </summary>
        /// <param name="command">The received command.</param>
        /// <param name="commandSpecificEventType">Type of the command specific event. See description of dwEventType at https://msdn.microsoft.com/en-us/library/windows/desktop/ms683241(v=vs.85).aspx</param>
        public void OnCommand(ServiceControlCommand command, uint commandSpecificEventType)
        {
            if (command == ServiceControlCommand.Stop)
            {
                statusReportCallback(ServiceState.StopPending, ServiceAcceptedControlCommandsFlags.None, win32ExitCode: 0, waitHint: 3000);

                var win32ExitCode = 0;

                try
                {
                    serviceImplementation.Stop();
                }
                catch
                {
                    win32ExitCode = -1;
                }

                statusReportCallback(ServiceState.Stopped, ServiceAcceptedControlCommandsFlags.None, win32ExitCode, waitHint: 0);
            }
        }
Example #3
0
        public void OnCommand(ServiceControlCommand command, uint commandSpecificEventType)
        {
            switch (command)
            {
            case ServiceControlCommand.Stop:
            {
                _statusReportCallback(ServiceState.StopPending, ServiceAcceptedControlCommandsFlags.None, win32ExitCode: 0, waitHint: 60000);
                var win32ExitCode = 0;
                try
                {
                    _serviceImplementation.Stop();
                }
                catch
                {
                    win32ExitCode = -1;
                }
                finally
                {
                    _statusReportCallback(ServiceState.Stopped, ServiceAcceptedControlCommandsFlags.None,
                                          win32ExitCode, waitHint: 0);
                }
                break;
            }

            case ServiceControlCommand.Pause:
            {
                _statusReportCallback(ServiceState.PausePending, ServiceAcceptedControlCommandsFlags.None, win32ExitCode: 0, waitHint: 20000);
                var win32ExitCode = 0;
                try
                {
                    _serviceImplementation.OnPause();
                }
                catch
                {
                    win32ExitCode = -1;
                }
                _statusReportCallback(ServiceState.Paused, ServiceAcceptedControlCommandsFlags.PauseContinue | ServiceAcceptedControlCommandsFlags.Stop, win32ExitCode, waitHint: 0);
                break;
            }

            case ServiceControlCommand.Continue:
            {
                _statusReportCallback(ServiceState.ContinuePending, ServiceAcceptedControlCommandsFlags.None, win32ExitCode: 0, waitHint: 20000);
                var win32ExitCode = 0;
                try
                {
                    _serviceImplementation.OnContinue();
                }
                catch
                {
                    win32ExitCode = -1;
                }
                if (_serviceImplementation.CanPauseAndContinue)
                {
                    _statusReportCallback(ServiceState.Running, ServiceAcceptedControlCommandsFlags.Stop | ServiceAcceptedControlCommandsFlags.PauseContinue, win32ExitCode, waitHint: 0);
                }
                else
                {
                    _statusReportCallback(ServiceState.Running, ServiceAcceptedControlCommandsFlags.Stop, win32ExitCode, waitHint: 0);
                }
                break;
            }
            }
        }