Example #1
0
        /// Stops the service. If any other services depend on this one for operation,
        /// they will be stopped first. The DependentServices property lists this set
        /// of services.
        public unsafe void Stop()
        {
            IntPtr serviceHandle = GetServiceHandle(Interop.mincore.ServiceOptions.SERVICE_STOP);

            try
            {
                // Before stopping this service, stop all the dependent services that are running.
                // (It's OK not to cache the result of getting the DependentServices property because it caches on its own.)
                for (int i = 0; i < DependentServices.Length; i++)
                {
                    ServiceController currentDependent = DependentServices[i];
                    currentDependent.Refresh();
                    if (currentDependent.Status != ServiceControllerStatus.Stopped)
                    {
                        currentDependent.Stop();
                        currentDependent.WaitForStatus(ServiceControllerStatus.Stopped, new TimeSpan(0, 0, 30));
                    }
                }

                Interop.mincore.SERVICE_STATUS status = new Interop.mincore.SERVICE_STATUS();
                bool result = Interop.mincore.ControlService(serviceHandle, Interop.mincore.ControlOptions.CONTROL_STOP, &status);
                if (!result)
                {
                    Exception inner = new Win32Exception(Marshal.GetLastWin32Error());
                    throw new InvalidOperationException(SR.Format(SR.StopService, ServiceName, _machineName), inner);
                }
            }
            finally
            {
                Interop.mincore.CloseServiceHandle(serviceHandle);
            }
        }
Example #2
0
        private unsafe void GenerateStatus()
        {
            if (!_statusGenerated)
            {
                IntPtr serviceHandle = GetServiceHandle(Interop.mincore.ServiceOptions.SERVICE_QUERY_STATUS);
                try
                {
                    Interop.mincore.SERVICE_STATUS svcStatus = new Interop.mincore.SERVICE_STATUS();
                    bool success = Interop.mincore.QueryServiceStatus(serviceHandle, &svcStatus);
                    if (!success)
                    {
                        throw new Win32Exception(Marshal.GetLastWin32Error());
                    }

                    _commandsAccepted = svcStatus.controlsAccepted;
                    _status           = (ServiceControllerStatus)svcStatus.currentState;
                    _type             = svcStatus.serviceType;
                    _statusGenerated  = true;
                }
                finally
                {
                    Interop.mincore.CloseServiceHandle(serviceHandle);
                }
            }
        }
Example #3
0
        /// Continues a service after it has been paused.
        public unsafe void Continue()
        {
            IntPtr serviceHandle = GetServiceHandle(Interop.mincore.ServiceOptions.SERVICE_PAUSE_CONTINUE);

            try
            {
                Interop.mincore.SERVICE_STATUS status = new Interop.mincore.SERVICE_STATUS();
                bool result = Interop.mincore.ControlService(serviceHandle, Interop.mincore.ControlOptions.CONTROL_CONTINUE, &status);
                if (!result)
                {
                    Exception inner = new Win32Exception(Marshal.GetLastWin32Error());
                    throw new InvalidOperationException(SR.Format(SR.ResumeService, ServiceName, _machineName), inner);
                }
            }
            finally
            {
                Interop.mincore.CloseServiceHandle(serviceHandle);
            }
        }