Пример #1
0
        private static NativeMethods.ServiceState GetServiceStatus(IntPtr service)
        {
            var status = new NativeMethods.ServiceStatus();

            if(NativeMethods.QueryServiceStatus(service, status) == 0)
            {
                throw new ServiceException("Failed to query service status.");
            }

            return status.dwCurrentState;
        }
Пример #2
0
        private static bool WaitForServiceStatus
            (IntPtr service, NativeMethods.ServiceState waitStatus, NativeMethods.ServiceState desiredStatus)
        {
            var status = new NativeMethods.ServiceStatus();

            NativeMethods.QueryServiceStatus(service, status);
            if(status.dwCurrentState == desiredStatus)
            {
                return true;
            }

            var dwStartTickCount = Environment.TickCount;
            var dwOldCheckPoint = status.dwCheckPoint;

            while(status.dwCurrentState == waitStatus)
            {
                var dwWaitTime = status.dwWaitHint / 10;

                if(dwWaitTime < 1000)
                {
                    dwWaitTime = 1000;
                }
                else if(dwWaitTime > 10000)
                {
                    dwWaitTime = 10000;
                }

                Thread.Sleep(dwWaitTime);

                if(NativeMethods.QueryServiceStatus(service, status) == 0)
                {
                    break;
                }

                if(status.dwCheckPoint > dwOldCheckPoint)
                {
                    dwStartTickCount = Environment.TickCount;
                    dwOldCheckPoint = status.dwCheckPoint;
                }
                else
                {
                    if((Environment.TickCount - dwStartTickCount) > status.dwWaitHint)
                    {
                        break;
                    }
                }
            }

            return status.dwCurrentState == desiredStatus;
        }
Пример #3
0
 private static void StopService(IntPtr service)
 {
     var status = new NativeMethods.ServiceStatus();
     NativeMethods.ControlService(service, NativeMethods.ServiceControl.Stop, status);
     var changedStatus = WaitForServiceStatus
         (service, NativeMethods.ServiceState.StopPending, NativeMethods.ServiceState.Stopped);
     if(!changedStatus)
     {
         throw new ServiceException("Unable to stop service");
     }
 }