protected void SetServiceStatus(ServiceState currentState, int win32ExitCode = 0, int serviceSpecificExitCode = 0, int checkPoint = 0, int waitHint = 30000)
        {
            if (DispatcherHandle != IntPtr.Zero)
            {
                ServiceStatus status = new ServiceStatus
                {
                    dwServiceType = ServiceType.OwnProcess,
                    dwCurrentState = currentState,
                    dwControlsAccepted = this.ControlsAccepted,
                    dwWin32ExitCode = win32ExitCode,
                    dwServiceSpecificExitCode = serviceSpecificExitCode,
                    dwCheckPoint = checkPoint,
                    dwWaitHint = waitHint
                };

                SetServiceStatus(DispatcherHandle, ref status);
            }

            if (currentState == ServiceState.Stopped)
            {
                Stopped.Set();
            }
            else
            {
                Stopped.Reset();
            }
        }
 private static extern bool SetServiceStatus(IntPtr hServiceStatus, ref ServiceStatus lpServiceStatus);
 protected static extern bool ControlService(
     NativeService hService,
     ServiceControl dwControl,
     out ServiceStatus lpServiceStatus
 );