unsafe void Stop(bool stopDependentServices) { using SafeServiceHandle serviceHandle = GetServiceHandle(Interop.Advapi32.ServiceOptions.SERVICE_STOP); if (stopDependentServices) { // 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.Advapi32.SERVICE_STATUS status = default; bool result = Interop.Advapi32.ControlService(serviceHandle, Interop.Advapi32.ControlOptions.CONTROL_STOP, &status); if (!result) { Exception inner = new Win32Exception(Marshal.GetLastWin32Error()); throw new InvalidOperationException(SR.Format(SR.StopService, ServiceName, _machineName), inner); } }
private unsafe void GenerateStatus() { if (!_statusGenerated) { IntPtr serviceHandle = GetServiceHandle(Interop.Advapi32.ServiceOptions.SERVICE_QUERY_STATUS); try { Interop.Advapi32.SERVICE_STATUS svcStatus = new Interop.Advapi32.SERVICE_STATUS(); bool success = Interop.Advapi32.QueryServiceStatus(serviceHandle, &svcStatus); if (!success) { throw new Win32Exception(Marshal.GetLastWin32Error()); } _commandsAccepted = svcStatus.controlsAccepted; _status = (ServiceControllerStatus)svcStatus.currentState; _type = svcStatus.serviceType; _statusGenerated = true; } finally { Interop.Advapi32.CloseServiceHandle(serviceHandle); } } }
/// <summary> /// Executes the command. /// </summary> /// <param name="command">The command</param> public unsafe void ExecuteCommand(int command) { using var serviceHandle = GetServiceHandle(Interop.Advapi32.ServiceOptions.SERVICE_USER_DEFINED_CONTROL); Interop.Advapi32.SERVICE_STATUS status = default; bool result = Interop.Advapi32.ControlService(serviceHandle, command, &status); if (!result) { Exception inner = new Win32Exception(Marshal.GetLastWin32Error()); throw new InvalidOperationException(SR.Format(SR.ControlService, ServiceName, MachineName), inner); } }
/// <summary> /// Continues a service after it has been paused. /// </summary> public unsafe void Continue() { using var serviceHandle = GetServiceHandle(Interop.Advapi32.ServiceOptions.SERVICE_PAUSE_CONTINUE); Interop.Advapi32.SERVICE_STATUS status = default; bool result = Interop.Advapi32.ControlService(serviceHandle, Interop.Advapi32.ControlOptions.CONTROL_CONTINUE, &status); if (!result) { Exception inner = new Win32Exception(Marshal.GetLastWin32Error()); throw new InvalidOperationException(SR.Format(SR.ResumeService, ServiceName, _machineName), inner); } }
private unsafe void GenerateStatus() { if (_statusGenerated) { return; } using var serviceHandle = GetServiceHandle(Interop.Advapi32.ServiceOptions.SERVICE_QUERY_STATUS); Interop.Advapi32.SERVICE_STATUS svcStatus = default; bool success = Interop.Advapi32.QueryServiceStatus(serviceHandle, &svcStatus); if (!success) throw new Win32Exception(Marshal.GetLastWin32Error()); _commandsAccepted = svcStatus.controlsAccepted; _status = (ServiceControllerStatus)svcStatus.currentState; _type = svcStatus.serviceType; _statusGenerated = true; }
/// Suspends a service's operation. public unsafe void Pause() { IntPtr serviceHandle = GetServiceHandle(Interop.Advapi32.ServiceOptions.SERVICE_PAUSE_CONTINUE); try { Interop.Advapi32.SERVICE_STATUS status = new Interop.Advapi32.SERVICE_STATUS(); bool result = Interop.Advapi32.ControlService(serviceHandle, Interop.Advapi32.ControlOptions.CONTROL_PAUSE, &status); if (!result) { Exception inner = new Win32Exception(Marshal.GetLastWin32Error()); throw new InvalidOperationException(SR.Format(SR.PauseService, ServiceName, _machineName), inner); } } finally { Interop.Advapi32.CloseServiceHandle(serviceHandle); } }